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 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 The JavaMail API requires the JavaBeansW Activation Framework, which can be downloaded at

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".


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 "" in our hosts file to point to Once again, 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.

package test;

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 = "";
		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("", pop3Host);
			Session emailSession = Session.getDefaultInstance(properties);

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

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

			Message[] messages = emailFolder.getMessages();
			for (int i = 0; i < messages.length; i++) {
				Message message = messages[i];
				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());

		} catch (NoSuchProviderException e) {
		} catch (MessagingException e) {
		} catch (IOException e) {



(Continued on page 2)

Page:    1 2 >