Aug 18, 2012

JavaMail API Part 1 - Sending simple text email

There are many applications where system has to send an email to the user.
  • Notification
  • Validation
  • Reporting
  • Alarming etc
Sending email though look simple but it has many things to be considered like 
  • Whether email is a rich text or simple text
  • Whether email has attachment
  • Email using authentication
  • Emailing using secured protocols and mechanisms
Let's first take a simple case of sending a simple text email. We can write a simple standalone java application to send email. 

Prerequisites


We will need  JavaMail API and Java Activation Framework (JAF). You can download latest version from Oracle website.

Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH.

We will also need mail server to send an email. There are many open source mail server we can use or we can use inbuilt mail server from operating system. Let's assume that mail server is running on localhost.

Sending Text Email


Following is the code.

// File Name SendMail.java

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

public class SendMail
{
   public static void main(String [] args)
   {    
      // Recipient's email ID needs to be mentioned.
      String to = "abcd@gmail.com";
      // Sender's email ID needs to be mentioned
      String from = "web@gmail.com";
      // Assuming you are sending email from localhost
      String host = "localhost";
      // Get system properties
      Properties properties = System.getProperties();
      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);
      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);
         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));
         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
         // Set Subject: header field
         message.setSubject("This is the Subject Line!");
         // Now set the actual message
         message.setText("This is actual message");
         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

Compile and run this program to send a simple email:

$ java SendMail
Sent message successfully....


Sending Email to multiple recipient


If you want to send an email to multiple recipients then following methods would be used to specify multiple email IDs:

void addRecipients(Message.RecipientType type, Address [] addresses) MessagingException

Here is the description of the parameters:

  • type: This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. Example Message.RecipientType.TO
  • addresses: This is the array of email ID. You would need to use InternetAddress() method while specifying email IDs

No comments: