Thursday, March 14, 2013

Send Mail From JSP Page......

Implementing a mail service is probably dream of every software developer,so i would like to exhibit the way of implementing this into your java web application.

So to develop this application first you should have is netbean ide 6.1 or greater version then it,if you have ain't have it acquire from her NetBean IDE  proper collection of jar files that will be needed to run your application.The jar file you should have are: mail.jar,mailapi.jar,smtp.jar,pop3.jar,activation .jar.

Executes the step given below properly to view your desired output.

1.Open your netbean ide:
2.Create a web application with the name of JavaMail:




3. Writing source code:
Open your project locate index.jsp (default page) and copy/paste below code in it(index.jsp):

<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title> Java Mail </title>
 </head>
 <body>
 <form action="sendMail.jsp" method="POST">
  <table border="0" align="center" cellpadding="5">
 <tbody>
  <thead><tr> <td colspan="3" align="center">
  <b> Send Mail </b> </td> </tr> </thead>
  <tr>
 <td> To </td> <td> : </td>
 <td> <input type="text" name="to" value="" /> </td>
 </tr>
 <tr>
<td> Subject </td> <td> : </td>
<td> <input type="text" name="subject" value="" /> </td>
</tr>
 <tr>
 <td> Message </td> <td> : </td>
 <td> <textarea name="message" rows="8" cols="30">
 </textarea></td>
 </tr>
 <tr>
 <td colspan="3" align="center">
 <input type="submit" value="Send Mail" />
                           
 <input type="reset" value="Reset" />
  <td>
   </tr>
 </tbody>
  </table>
 </form>
 </body>
</html>

the above code is simple html form asking for a receiver's email address,subject body and message body.
your form would look some thing like this

Now right click on your project and add java class Mail with package name jMail.
now open that java(Mail) class and place the following code:

//importing packages

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

//declare the variables at global levels
String to;
String from;
String message;
String subject;
String smtpServ; 

//create getter  setter of the variables declared at class level
public String getTo() 
{
        return to;
}

public void setTo(String to) 
{
        this.to = to;
}

public String getFrom() 
{
        return from;
}

public void setFrom(String from) 
{
        this.from = from;
}

public String getMessage() 
{
        return message;
}

public void setMessage(String message) 
{
        this.message = message;
}

public String getSubject() 
{
        return subject;
}

public void setSubject(String subject) 
{
        this.subject = subject;
}

public String getSmtpServ() 
{
        return smtpServ;
}

public void setSmtpServ(String smtpServ) 
{
        this.smtpServ = smtpServ;
}

//to generate getter setter implicitly just right click on page > refactor > encapsulate field.Then have a glance at all getter setter methods and click refactor to generate the getter and setter.

now you need to add jar file your project as mentioned above.For that right click on project node(JavaMail).
Now go to libraries tag for adding it.

 Click on the ADD JAR/Folder.Browse for your jar file and attach in your project.Jar file that should be added are:
  • activation.jar
  • pop3.jar
  • mail.jar
  • mailapi.jar
  • smtp.jar
If you do not perform this step, then NetBeans will not recognize JAF and JavaMail classes and hence will not compile the program given below which makes use of classes such as Message, Session, Transport etc.

Compile your program to check whether you have been able to successfully include these jar files or not.

//continue with source code 
public int sendMail(){
        try
        {
            Properties props = System.getProperties();
              // -- Attaching to default Session, or we could start a new one --
              props.put("mail.transport.protocol", "smtp" );
              props.put("mail.smtp.starttls.enable","true" );
              props.put("mail.smtp.host",smtpServ);
              props.put("mail.smtp.auth", "true" );
              Authenticator auth = new SMTPAuthenticator();
              Session session = Session.getInstance(props, auth);
              // -- Create a new message --
              Message msg = new MimeMessage(session);
              // -- Set the FROM and TO fields --
              msg.setFrom(new InternetAddress(from));
              msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
              msg.setSubject(subject);
              msg.setText(message);
              // -- Set some other header information --
              msg.setHeader("MyMail", "Mr. XYZ" );
              msg.setSentDate(new Date());
              // -- Send the message --
              Transport.send(msg);
              System.out.println("Message sent to"+to+" OK." );
              return 0;
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
          System.out.println("Exception "+ex);
          return -1;
        }
  }
//above code is to initialize all jar file in your project
private class SMTPAuthenticator extends javax.mail.Authenticator {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            String username =  "";        // specify your email id here (sender's email id)
            String password = "";                                      // specify your password here
            return new PasswordAuthentication(username, password);
        }

Save the file and compile it.

Now add a new jsp page with the name sendmail.jsp and copy/paste the following code:
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
  <title>JSP Page</title>
 </head>
 <body>
<jsp:useBean id="mail" scope="session" class="jmail.mail" />
<jsp:setProperty name="mail" property="to" param="to" />
<jsp:setProperty name="mail" property="from" value="Java.Mail.CA@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="message" param="message" />
 <%
String to = mail.getTo();
int result;
result = mail.sendMail();
if(result == 0){
    out.println(" Mail Successfully Sent to "+to);//prints when mail is successfully delivered to the receiver
}
else{
    out.println(" Mail NOT Sent to "+to);//prints when there is failure in sending the mail to receiver
}  
%>
</body>
</html>


Now compile the whole application and run it.
Key points to be remembered  while  developing this application:
1.Import proper jar file which is the base part of the this application.
2.Don't forget to provide the name and password in java file.
3.and last but not least if in any circumstances  there is failure of sending mail,check your proxy stting in netbeans and reconfigure it as per your machines proxy setting.

Click Here for demo there's a something additional in the demo have a look at it.

Here i conclude for this topic,any doubts regarding,please notify me.  

Tuesday, March 12, 2013

Disable Right Click On Web Page

Sometime you want secure webpages,you don't want  anyone to download your content,so for that you have to disable your right click control of your mouse.The below coding will help you out to make it work for your  web page:
----------------------------------------------------------------------------------------
<script language=JavaScript> 
var message="Function Disabled!";
 function clickIE4()
{
 if (event.button==2){ alert(message); return false; } }
 function clickNS4(e){ if (document.layers||document.getElementById&&!document.all)
{ if (e.which==2||e.which==3){ alert(message); return false; } } } 
if (document.layers){ document.captureEvents(Event.MOUSEDOWN); document.onmousedown=clickNS4; } 
else if (document.all&&!document.getElementById){ document.onmousedown=clickIE4; } document.oncontextmenu=new Function("alert(message);return false") 
</script>  
------------------------------------------------------------------------------------------
place the above code with in <HEAD> </HEAD> tag.

that's it,place the code and view the output.

Tuesday, March 5, 2013

History of Java


          Before starting with a anyother thing,i would like to introduce java to those people who are new to it and don't have any idea of incessant power of java.
                                                      
James gosling popularly know as father of java,with his other colleagues Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991.The language was initially called oak after an oak tree that stood outside Gosling's office; it went by the name Green later, and was later renamed Java, from Java coffee, said to be consumed in large quantities by the language's creators.The first version of java i.e java 1.0 in 1995 by the worlds renowned company Sun Microsystems with a promise of Write Once Run Anywhere(WORA).

Major web browsers soon incorporated the ability to run Java applets within web pages, and Java quickly became popular. With the advent of Java 2 (released initially as J2SE 1.2 in December 1998 – 1999), new versions had multiple configurations built for different types of platforms. For example, J2EE targeted enterprise applications and the greatly stripped-down version J2ME for mobile applications (Mobile Java). J2SE designated the Standard Edition. In 2006, for marketing purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.                                                                    


On January 10 2010, Sun Microsystems  was being acquired by Oracle Corporation,the third largest software maker after Big Blue(Microsoft) and IBM.And james gosling who dedicated himself to Sun Microsystems over a year is now working with Google Inc.
 

So,this hows java was being invented and was put to an operation for ease and fluent technology for peoples.