Send and read emails with JavaMail API

In this article, I’m going to talk about sending and receiving email with JavaMail API. There are two ways of doing it: manually or with application server such as glassfish.

Manually send email

I personally feel that JavaMail API is somewhat complicated, because there are many way to do the same thing and the components are overwriting each other. Plus, as you test the functions with mail services provided on internet, it’s sometimes hard to track what goes wrong. So, first I’m going to post a way that is tested to work well with my Gmail and Sina mail.

First, you need to import the library. If you’re using maven, then the dependencies would be like this:

 <build>
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.5.1</version>
             <configuration>
                 <source>1.8</source>
                 <target>1.8</target>
             </configuration>
         </plugin>
     </plugins>
 </build>

 <dependencies>
     <dependency>
         <groupId>javax.mail</groupId>
         <artifactId>javax.mail-api</artifactId>
         <version>1.5.5</version>
     </dependency>
     <dependency>
         <groupId>com.sun.mail</groupId>
         <artifactId>javax.mail</artifactId>
         <version>1.5.5</version>
     </dependency>
 </dependencies>

There are 2 parts you need to modify for your pom.xml file. The build part is just for me to specify the jdk version, because I’m using some new features of jdk 1.8, and by diffault it’s jdk 1.5.

The javax.mail.javax.mail-api is interface and com.sum.mail.javax.mail is the implementation. I listed them because of their relationship. However, you don’t really need the first one since the interfaces are also included in com.sum.mail. Note there are other implementations you can find.

The java code is like this:

import java.io.File;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class TestSend {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        Session session = Session.getInstance(props);

/* The comment out part not working */
//      Session session = Session.getInstance(props, new Authenticator() {
//          @Override
//          protected PasswordAuthentication getPasswordAuthentication() {
//              return new PasswordAuthentication("username", "password");
//          }
//      });

        Message message = new MimeMessage(session);
        Address myAddress = new InternetAddress("from@sender.com", "sender");
        Address recipient = new InternetAddress("to@recipient.com");
        message.setFrom(myAddress);
        message.setRecipient(Message.RecipientType.TO, recipient);
        message.setSubject("Subject");
        message.setText("This will be overwrite by msgText");
		
        String msgText = "The text to send";

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(msgText, "text/plain");

        MimeBodyPart fileBodyPart = new MimeBodyPart();
        fileBodyPart.attachFile(new File("filepath")); // This line shows how to attach a file
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        multipart.addBodyPart(fileBodyPart);

        message.setContent(multipart);

        Transport t = session.getTransport("smtps"); // most email service providers don’t support smtp anymore. This is where the most tricks come from.
        t.connect("smtp.sender.com", "username", "password"); // in most case, the username is the first part (left part of @) of your primary email address, if you have more than one bound to your account.

        t.sendMessage(message, message.getAllRecipients()); // It looks for an array. So, you can do this way, or just put all addresses into an array, then pass in.

        t.close();
    }
}

I instantiated a Properties() instance, but had never set anything in it. If you would like to store the configurations with Properties(), please refer the property settings which will discuss in the following sections. You can also find all the property keywords in JavaMail API documentation. Furthermore, I want to mention that the Authenticator class is abstract, but there is no unimplemented method. The way they are doing this is to let you overwrite getPasswordAuthentication() method which is the only method not marked as final in this class. If you look into the source code, you will find the original one is returning null, which would be good for if the mail provider doesn’t ask for a password or any kind of authentication. But in most cases, a PasswordAuthentication class should be returned with your username and password.

Manually read email/check inbox

As you see, the connection setup is very similar between sending and receiving. You need to change protocol and configure the store. Then connect to the server and download emails from a particular folder. Note that, for pop3 protocol, the folder is not really something you need to choose, because the server will not be syncing the folders on your client/java program. On the other hand, imap will sync folders, so you want to take care of it.
In addition, Message class has several methods to specifically pick up the certain part of your email, e.g. header, body, etc. In my code list, you can see how it works.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class TestReceive {

    public static void main(String[] args) throws Exception {

        Properties props = new Properties();
        props.put("mail.pop3.host", "pop.domain.com");
        props.put("mail.store.protocol", "pop3");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password");
            }
        });

        Store store = session.getStore("pop3");

        store.connect();

        Folder inbox = store.getFolder("INBOX");

        inbox.open(Folder.READ_ONLY);

        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            System.out.println("Email subject: " + message.getSubject());

            switch (message.getContentType().split(";")[0]) {
            case "text/plain":
                System.out.println("Body: " + message.getContent());
                break;

            case "multipart/mixed":
                Multipart parts = (Multipart) message.getContent();

                for (int i = 0; i < parts.getCount(); i++) {
                    BodyPart part = parts.getBodyPart(i);

                    if (part.getDisposition() == null) {
                        System.out.println("Body: " + part.getContent());
                        continue;
                    }

                    switch (part.getDisposition()) {
                    case Part.INLINE: // a signal to tell Browser/MUA to display it if possible
                        saveFile("misc/display/" + part.getFileName(), part.getInputStream());
                        break;

                    case Part.ATTACHMENT: // a signal to let Browser/MUA to just download it
                        saveFile("misc/store/" + part.getFileName(), part.getInputStream());
                        break;

                    default:
                        System.out.println("unknown disposition");
                        break;
                    }
                }
            break;
            default:
                System.out.println("unknown type");
                break;
            }
        }
        inbox.close(false);
        store.close();
    }

    private static void saveFile(String path, InputStream inputStream) {
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
                BufferedInputStream bis = new BufferedInputStream(inputStream)) {
            byte[] b = new byte[1024];
            while (-1 != bis.read(b))
                bos.write(b);
			
            bos.write(b); // write the the last part which contains less than 1024 bytes data.

            bos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Send Email with GlassFish

To send and receive emails with GlassFish, you need to configure JavaMail Sessions, as well as modify some java code. If you haven’t setup a GlassFish server, then you need to do it first. For setting up GlassFish Application Server, see here.

If you have followed along with this article, you should have a simple maven project. To use GlassFish, you need to turn your project into a deployable project, e.g. Dynamic Web Module. To do that, you should first convert your project to faceted form. Go to the project Properties -> Project Facets -> Convert to faceted form. Then select Dynamic Web Module (DWM), and hit Apply butto. Then you select GlassFish Web Extensions, and click Apply.
Optionally, you can specify the targeted runtime. Still in the project Property window, go to Targeted Runtimes, select a GlassFish server you want to deploy to, and apply. Then, go to Server, choose the server, and apply.

Configure GlassFish

Go to GlassFish web console (localhost:4848) -> Resources -> JavaMail Sessions. Create a new session, and configure it as follow (use gmail as an example):

JNDI Name: mail/gmail (name for JNDI lookup)
Mail Host: smtp.gmail.com
Default User: username
Default Sender Address: username@gmail.com (or other email address bound to your account)
Deployment Order: 100 (default: 100)
Description: You can leave it blank or put whatever
Status: check Enabled

Advanced (You can leave them as default, unless the protocols are different)
Store Protocol: imap
Store Protocol Class: com.sun.mail.imap.IMAPStore (You can use auto-complete function in Eclipse IDE to check com.sun.mail package to find more classes)
Transport Protocol: smtp
Transport Protocol Class: com.sun.mail.smtp.SMTPTransport
Debug: uncheck Enabled

Additional Properties
mail.smtp.password:password
mail.smtp.auth:true (indicates the mail server asks for authentication)
mail.smtp.port:587 (If you tried configuring your SMTP server on port 465 (with SSL/TLS) and port 587 (with STARTTLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL/TLS). )
mail.smtp.starttls.enable:true (corresponds with port 587. For example, if you use port 465, then you need to set mail.smtp.ssl.enable:true )

Note: The protocol portion in property name (e.g. mail.smtp.something) must be as exact the same as the corresponding protocol. Otherwise, they’ll not be picked up by the program.

Java Code

There is no big change in Java code. Instead of running everything within the main method, I put the code in a method called send, and set up a timer to call it. In addition, I use resource injection to use the configurations I just set in GlassFish.

import java.io.File;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

@Singleton
public class TestSend {

    @Resource(lookup = "mail/gmail")
    private Session session;

    @Schedule(second = "0", minute = "*/2", hour = "*") // The simplest way to define a Schedule, you have to have at least second, minute and hour variables of Schedule annotation
    public void timer() {
        send();
    }

    public void send() {

        System.out.println("Sending Start");

        try { // if you just throw out the exceptions, you will not get a message since this method is called by a Schedule
            Message message = new MimeMessage(session);
            Address recipient = new InternetAddress("ljz0863@sina.com");
            message.setRecipient(Message.RecipientType.TO, recipient);
            message.setSubject("Subject");
            message.setText("This will be overwrite by msgText");

            String msgText = "The text to send";

            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(msgText, "text/plain");

            MimeBodyPart fileBodyPart = new MimeBodyPart();
            fileBodyPart.attachFile(new File(getClass().getClassLoader().getResource("logo.png").toURI())); // .getResource() method this actually look up all the files in the domain directory, and find whatever matches the given pattern
            // fileBodyPart.attachFile(new File(Thread.currentThread().getContextClassLoader().getResource("logo.png").toURI())); // Another way of targeting the resource
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            multipart.addBodyPart(fileBodyPart);

            message.setContent(multipart);

            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("End Sending");
        }
    }
}

Read Email with GlassFish

Configure GlassFish

Go to GlassFish web console (localhost:4848) -> Resources -> JavaMail Sessions. Create a new session, and configure it as follow (use gmail as an example):

JNDI Name: mail/gmail
Mail Host: imap.gmail.com
Default User: username
Default Sender Address: username@gmail.com
Deployment Order: 100
Description: You can leave it blank or put whatever
Status: check Enabled

Advanced (You can leave them as default, unless the protocols are different)
Store Protocol: imap
Store Protocol Class: com.sun.mail.imap.IMAPStore
Transport Protocol: smtp
Transport Protocol Class: com.sun.mail.smtp.SMTPTransport
Debug: uncheck Enabled

Additional Properties
mail.imap.ssl.enable:true
mail.imap.password:password

Java Code

For the Java code, I set it to just display the subjects of my emails.

import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

@Singleton
public class TestReceive {

    @Resource(lookup = "mail/sina")
    private Session session;

    @Schedule(second = "*/30", minute = "*", hour = "*")
    public void receive() {

        System.out.println("Receiving Start");

        try {
            Store store = session.getStore();

            store.connect();

            Folder inbox = store.getFolder("INBOX");

            inbox.open(Folder.READ_ONLY);

            Message[] messages = inbox.getMessages();

            for (Message message : messages)
                System.out.println("Email subject: " + message.getSubject());

            inbox.close(false);
            store.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("End Receiving");
        }
    }
}
Advertisements

One thought on “Send and read emails with JavaMail API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s