March 6, 2021
Hot Topics:

Use JavaMail to Automate Email Distribution

  • 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 ();
    private void sendEmail()
            //This is required to load all the properties
           FileInputStream fileInputStream = new FileInputStream(emailInfo);
        }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);
           MimeMessage mimeMessage = new MimeMessage(session);
           mimeMessage.setSentDate(new Date());
           System.out.println("Sending e-mail...");
           System.out.println("e-mail sent.");
        catch(MessagingException me)
           System.out.println("e-mail send failed.");

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:

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.

Page 1 of 2

This article was originally published on February 19, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date