logo

Sending Email Through JavaMail API In Servlet


Show

JavaMail API provides many Classes that you can use to send Emails from java. The javax.mail and javax.mail.internet packages have all the classes needed for sending and receiving emails.

If you want a better understanding of this example click here the steps for sending an email from JavaMail API.

For sending the email by making use of JavaMail API, you are needed to load two jar files:

  • mail.jar
  • activation.jar

download these jar files or you can go to the oracle site to download the latest version.

Sending Email through JavaMail API in Servlet, Here is the Example

To make a simple example of sending an email from the servlet. For this example we are going to create 3 files:

  • Index.html file for input
  • SendMail.java is a servlet file for handling the request and giving the response to the user. It uses the Send Method of the mailer class to send the email.
  • Mailer.java is a java class that contains send method to send the emails to the mentioned Recipient(receiver)

Index.html

<form action="servlet/SendMail">  
To:<input type="text" name="to"/><br/>  
Subject:<input type="text" name="subject"><br/>  
Text:<textarea rows="10" cols="70" name="msg"></textarea><br/>  
<input type="submit" value="send"/>  
</form>

SendMail.java

import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
  
public class SendMail extends HttpServlet {  
public void doGet(HttpServletRequest request,  
 HttpServletResponse response)  
    throws ServletException, IOException {  
  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
      
    String to=request.getParameter("to");  
    String subject=request.getParameter("subject");  
    String msg=request.getParameter("msg");  
          
    Mailer.send(to, subject, msg);  
    out.print("message has been sent successfully");  
    out.close();  
    }  
  
}  

Mailer.java

import java.util.Properties;  
  
import javax.mail.*;  
import javax.mail.internet.InternetAddress;  
import javax.mail.internet.MimeMessage;  
  
public class Mailer {  
public static void send(String to,String subject,String msg){  
  
final String user="sonoojaiswal@javatpoint.com";//change accordingly  
final String pass="xxxxx";  
  
//1st step) Get the session object    
Properties props = new Properties();  
props.put("mail.smtp.host", "mail.javatpoint.com");//change accordingly  
props.put("mail.smtp.auth", "true");  
  
Session session = Session.getDefaultInstance(props,  
 new javax.mail.Authenticator() {  
  protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication(user,pass);  
   }  
});  
//2nd step)compose message  
try {  
 MimeMessage message = new MimeMessage(session);  
 message.setFrom(new InternetAddress(user));  
 message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
 message.setSubject(subject);  
 message.setText(msg);  
   
 //3rd step)send message  
 Transport.send(message);  
  
 System.out.println("Done");  
  
 } catch (MessagingException e) {  
    throw new RuntimeException(e);  
 }  
      
}  
}  

download this example (developed without IDE)

download this example (developed using Eclipse IDE)

download this example (developed using Netbeans IDE)