How do I receive email in Java?
Author: Deron Eriksson
Description: This Java tutorial describes receiving an email in Java.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

In another tutorial, we set up a local email server using the Standard Edition of MailEnable, which can be obtained from http://www.mailenable.com/. After doing so, we sent an email using a JavaSW class to an email account on the email server.

Like the other tutorial, this tutorial utilizes the JavaMail API, which can be downloaded at http://java.sun.com/products/javamail/downloads/. The JavaMail API requires the JavaBeansW Activation Framework, which can be downloaded at http://java.sun.com/products/javabeans/jaf/downloads/index.html.

I added the mail.jar and activation.jar files to my project's lib directory. I added version numbers to the jarW file names to make it easier to keep track of the versions of the jar files. After that, I added the jar files to my project's build path. The project has one Java class, EmailReceiveTest.

'testing' project

For this tutorial, I sent a total of 3 emails to the "bigcakes" account on the local email server. I cc'd the 3 emails to "littlecakes".

MailEnable

The EmailReceiveTest class will contact the local email server and read and display the messages belonging to the "bigcakes" account. In the other tutorial, we set up "cakelycakes.com" in our hosts file to point to 127.0.0.1. Once again, cakelycakes.com is used as the host name for our email server. The mail store type used is POP3. The EmailReceiveTest class uses the user name "bigcakes" with the password "bigcakes" to connect to the POP3 email server. (SMTP is the 'send email' protocol used in the other tutorial. POP3 is the 'receive email' protocol used in this tutorial.)

The POP3 host name is placed in a properties object and the properties object is fed to the Session.getDefaultInstance() method as a parameter when we obtain our email session object. We then retrieve a POP3Store object from the session, which is used to connect to the email server using the "bigcakes" name and password.

The INBOX folder for "bigcakes" is retrieved from the store, and it is opened in READ_ONLY mode. Following this, an array of email Message objects is retrieved from the folder. The subject, from address, and message text is displayed for each message. Following this, the folder is closed (with "false" as a parameter to ensure the messages aren't deleted from the server), and the store is closed.

EmailReceiveTest.java

package test;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;

import com.sun.mail.pop3.POP3Store;

public class EmailReceiveTest {

	public static void main(String[] args) {

		String mailPop3Host = "cakelycakes.com";
		String mailStoreType = "pop3";
		String mailUser = "bigcakes";
		String mailPassword = "bigcakes";

		receiveEmail(mailPop3Host, mailStoreType, mailUser, mailPassword);
	}

	public static void receiveEmail(String pop3Host, String storeType, String user, String password) {

		try {
			Properties properties = new Properties();
			properties.put("mail.pop3.host", pop3Host);
			Session emailSession = Session.getDefaultInstance(properties);

			POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
			emailStore.connect(user, password);

			Folder emailFolder = emailStore.getFolder("INBOX");
			emailFolder.open(Folder.READ_ONLY);

			Message[] messages = emailFolder.getMessages();
			for (int i = 0; i < messages.length; i++) {
				Message message = messages[i];
				System.out.println("==============================");
				System.out.println("Email #" + (i + 1));
				System.out.println("Subject: " + message.getSubject());
				System.out.println("From: " + message.getFrom()[0]);
				System.out.println("Text: " + message.getContent().toString());
			}

			emailFolder.close(false);
			emailStore.close();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

(Continued on page 2)

Page:    1 2 >