August 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Use JavaMail to Automate Email Distribution

  • February 19, 2010
  • By Sridhar M S
  • Send Email »
  • More Articles »

With software automating every action possible, why not automate the distribution of pertinent information to end-users/customers via email? The JavaMail API (currently in version 1.4.3) can support this development in a protocol-independent manner and remain platform independent as always.

Let's skip the boring theory and jump right into a demo program, which you can use to send an email with a predetermined set of information. Here's the JavaMail code for sending the email:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
 
class EmailClient
{
    final String emailInfo = "EmailInfo.properties";
    Properties properties = new Properties();
 
    public static void main(String args[])
    {
        EmailClient emailClient = new EmailClient ();
        emailClient.sendEmail();
    }
 
    private void sendEmail()
    {
       try{
            //This is required to load all the properties
           FileInputStream fileInputStream = new FileInputStream(emailInfo);
           properties.load(fileInputStream);
           fileInputStream.close();
        }catch(IOException ioe)
       {
            //throw IOException of your choice.
            //can end here
       }
        System.out.println("Email properties read successfully.");
 
       String smtpAddress = properties.getProperty("smtpAddress");
       String fromAddress = properties.getProperty("fromAddress");
       String toAddress = properties.getProperty("toAddress");
       String emailSubject = properties.getProperty("emailSubject");
       String emailBody = properties.getProperty("emailBody");
 
       Properties props = new Properties();
        props.put("mail.smtp.host", smtpAddress);
        props.put("mail.from", fromAddress);
       Session session = Session.getInstance(props, null);
 
       try
       {
           MimeMessage mimeMessage = new MimeMessage(session);
           mimeMessage.setRecipients(Message.RecipientType.TO,toAddress);
           mimeMessage.setSentDate(new Date());
           mimeMessage.setSubject(emailSubject);
           mimeMessage.setText(emailBody);
           System.out.println("Sending e-mail...");
           Transport.send(mimeMessage);
           System.out.println("e-mail sent.");
       }
        catch(MessagingException me)
       {
           System.out.println("e-mail send failed.");
           me.getMessage();
       }
    }
}


The code is fairly simple to understand:

  1. We have loaded the properties for the email to be sent from a properties file. This could come from any other source, such as hardcoded values, a database, a user interface, etc.
  2. The processed information is updated to the MimeMessage object, which is the actual email object. (The MIME, or Multipurpose Internet Mail Extensions, is an Internet standard that specifies how messages must be formatted for interoperability.)
  3. MimeMessage takes a session argument, which can either be created (as in this application) or refer to the default session.
  4. The session—important in any context and used here for the email—is established using the properties mail.smtp.host and mail.from.
  5. We set other details, such as the date, subject, and the body of the email, which will be used when the email is sent.
  6. Finally, the email is dispatched using the send() method of the Transport class.

The entire email information is made available in a properties file. Here are the contents of the properties file for this application:

smtpAddress=smtp.server.address
fromAddress=myself@me.com
toAddress=someone@you.com
emailSubject=First email
emailBody=My first email


As stated previously, the source of the required information can vary. In this case, we can modify the entries at will without recompiling the code to experiment. By the way, holding configurable information in properties files is a standard programming practice.


Tags: email, Java SE, JavaMail



Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel