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.
<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.*;
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):
<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.