Sending Email through JavaMail API in Servlet

Sending Email through JavaMail API in Servlet

In this article, I am going to discuss how to send Email through JavaMail API in Servlet. Please read our previous article, where we discussed How to upload and upload files to the server in servlet. At the end of this article, you will understand how to use JavaMail API to send emails using an SMTP server. For TLS and SSL authentication, we are going to use the Email SMTP server because it supports both. JavaMail API is not a part of standard JDK, so we have to download it from its official website. Download the latest version of the JavaMail reference implementation and include it in your project build path. The jar file name will be:

  1. javax.mail.jar
  2. javax.activation.jar

The example contains the following steps:

  1. Creating javax.mail.Session object
  2. Creating javax.mail.internet.MimeMessage object, we need to set different properties in this object such as recipient email address, Email Subject, Reply-To email, email body, etc.
  3. Using javax.mail.Transport to send the email message.

The logic to create a session differs based on the type of SMTP server, for example, if the SMTP server doesn’t require any authentication, we can create the Session object with some simple properties whereas if the SMTP server requires TLS or SSL authentication, then the logic to create will differ. Here, I am setting some header properties in the MimeMessage, which are used by the email clients to properly render and display the email message. Here, I am using Session.getInstance() to get the Session object by passing the Properties object. We need to set the mail.smtp.host property with the SMTP server host. If the SMTP server is not running on the default port (25), then we need to set mail.smtp.port property.

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

public class SendEmail {
 public static void main(String[] args) {

  String host = "smtp.gmail.com";
  final String user = "myemail@gmail.com";// change accordingly
  final String password = "xxxxx";// change accordingly

  String to = "toemail@gmail.com";// change accordingly

  // Get the session object
  Properties props = new Properties();
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.auth", "true");
  // props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", "587");
  Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(user, password);
   }
  });

  // Compose the message
  try {
   MimeMessage message = new MimeMessage(session);
   message.setFrom(new InternetAddress(user));
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
   message.setSubject("JAVAMailAPI");
   message.setText("This is simple program of sending email using JavaMail API");

   // send the message
   Transport.send(message);

   System.out.println("message sent successfully...");

  } catch (MessagingException e) {
   e.printStackTrace();
  }
 }
}
Output

Run your code to get the following output on the console and check your email.

Sending Email through JavaMail API in Servlet

You will get the following email:

How to send email through JavaMail API in Servlet

In the next article, I am going to discuss how to write data into PDF using servlet. Here, in this article, we develop an application to send email through JavaMail API in Servlet and I hope you enjoy this How to send email through JavaMail API in Servlet article.

Leave a Reply

Your email address will not be published.