Receiving email in Java

Example of receiving email in Java provides examples of sending, receiving, forwarding and deleting emails, Learn how to receive email in java

Receiving email in Java

Receiving email in Java

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

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 in Java

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

  1. import java.io.IOException;  
  2. import java.util.Properties;  
  3. import javax.mail.Folder;  
  4. import javax.mail.Message;  
  5. import javax.mail.MessagingException;  
  6. import javax.mail.NoSuchProviderException;  
  7. import javax.mail.Session;  
  8. import com.sun.mail.pop3.POP3Store;  
  9.   
  10. public class ReceiveMail{  
  11.   
  12.  public static void receiveEmail(String pop3Host, String storeType,  
  13.   String user, String password) {  
  14.   try {  
  15.    //1) get the session object  
  16.    Properties properties = new Properties();  
  17.    properties.put("mail.pop3.host", pop3Host);  
  18.    Session emailSession = Session.getDefaultInstance(properties);  
  19.      
  20.    //2) create the POP3 store object and connect with the pop server  
  21.    POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);  
  22.    emailStore.connect(user, password);  
  23.   
  24.    //3) create the folder object and open it  
  25.    Folder emailFolder = emailStore.getFolder("INBOX");  
  26.    emailFolder.open(Folder.READ_ONLY);  
  27.   
  28.    //4) retrieve the messages from the folder in an array and print it  
  29.    Message[] messages = emailFolder.getMessages();  
  30.    for (int i = 0; i < messages.length; i++) {  
  31.     Message message = messages[i];  
  32.     System.out.println("---------------------------------");  
  33.     System.out.println("Email Number " + (i + 1));  
  34.     System.out.println("Subject: " + message.getSubject());  
  35.     System.out.println("From: " + message.getFrom()[0]);  
  36.     System.out.println("Text: " + message.getContent().toString());  
  37.    }  
  38.   
  39.    //5) close the store and folder objects  
  40.    emailFolder.close(false);  
  41.    emailStore.close();  
  42.   
  43.   } catch (NoSuchProviderException e) {e.printStackTrace();}   
  44.   catch (MessagingException e) {e.printStackTrace();}  
  45.   catch (IOException e) {e.printStackTrace();}  
  46.  }  
  47.   
  48.  public static void main(String[] args) {  
  49.   
  50.   String host = "mail.hpnmaratt.com";//change accordingly  
  51.   String mailStoreType = "pop3";  
  52.   String username= "sam@javatpoint.com";  
  53.   String password= "xxxxx";//change accordingly  
  54.   
  55.   receiveEmail(host, mailStoreType, username, password);  
  56.   
  57.  }  

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