How To Receive Emails In Java

Introduction 

 
This article explains how to receive emails using the JavaMail API and a basic example.
 

About JavaMail API

 
All of the basics about JavaMail API can be read from my previous article, in which all of the concepts, definitions, packages, and classes have been explained in detail. They are available at the following link:
 
 
The following explains how to receive mail using the JavaMail API.
 
When receiving mail, we need to use all of the core classes of packages used, such as javax.mail, javax.mail.internet, javax.mail.activation, java.io, and so on.
 
When we receive mail, we sometimes receive attachments with the mail.  That is done using the multipart and BodyPart classes.
 
Also, when receiving mail, we need to load the following two jar files:
  • Mail.jar
  • Activation.jar
Without these jar files, your packages (javax.mail and javax.mail.internet) will not work and the mail will not be received. Both of the jar files have been attached to the previous article, where you can download those files.  You can also download the files from the Oracle website.
 
Now, concentrate on the following example of receiving mail, using the JavaMail API. 
 

Example of receiving mail

 
Here we created a few classes that are different from those used in the example of sending mail. So, read and analyse the differences between the two.
  1. import javax.mail.*;  
  2. import javax.activation.*;  
  3. import javax.mail.internet.*;  
  4. import java.util.*;  
  5. import java.io.*;  
  6. class ReadAttachment {  
  7.  public static void main(String agrs[]) throws Exception {  
  8.   String host = "mail.c-sharpcorner.com";  
  9.   final String user = "gopichand@c-sharpcorner.com";  
  10.   final String password = "xxxxxxx";  
  11.   Properties properties = System.getProperties();  
  12.   properties.setProperty("mail.smtp.host", host);  
  13.   properties.put("mail.smtp.auth""true");  
  14.   Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {  
  15.    protected PasswordAuthentication getPassworAuthentication() {  
  16.     return new PasswordAuthentication(user, password);  
  17.    }  
  18.   });  
  19.   Store store = session.getStore("pop3");  
  20.   store.connect(host, user, password);  
  21.   Folder folder = store.getFolder("inbox");  
  22.   folder.open(Folder.READ_WRITE);  
  23.   Message[] message = folder.getMessages();  
  24.   for (int i = 0; i < message.length; i++) {  
  25.    System.out.println("-------" + (i + 1) + "-------");  
  26.    System.out.println(message[i].getSentDate());  
  27.    Multipart multipart = (Multipart) message[i].getContent();  
  28.    for (int j = 0; j < multipart.getCount(); j++) {  
  29.     BodyPart bodyPart = multipart.getBodyPart(i);  
  30.     InputStream stream = bodyPart.getInputStream();  
  31.     BufferedReader br = new BufferedReader(new InputStreamReader(stream));  
  32.     while (br.ready()) {  
  33.      System.out.println(br.readLine());  
  34.     }  
  35.     System.out.println();  
  36.    }  
  37.    System.out.println();  
  38.   }  
  39.   folder.close(true);  
  40.   store.close();  
  41.  }  
  42. }   
This will help to receive emails with an attachment.