home page

Receiving email using JavaMail API

For receiving email Store and Folder classes are used in collaboration with MimeMessage, Session and Transport classes.

For better understanding of this example, learn the steps of sending email using JavaMail API first.

For sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • activation.jar
download these jar files (or) go to the Oracle site to download the latest version.

Steps for receiving the email using JavaMail API

There are 5 steps to receive the email using JavaMail API. They are:

  1. Get the session object
  2. create the POP3 store object and connect with the pop server
  3. create the folder object and open it
  4. retrieve the messages from the folder in an array and print it
  5. close the store and folder objects

Example of Receiving email using JavaMail API

Let's see the example of receiving the mail using java mail api.

As you can see in the above example, userid and password need to be authenticated. As, this program illustrates, you can receive email easily but change the username and password accordingly. Let's see how to run it once again by simple technique:

Load the jar file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac ReceiveMail.java
run by c:\> java Receivemail