Getting email utilizing JavaMail API

For accepting email Store and Folder classes are utilized as a part of joint effort with MimeMessage, Session and Transport classes.

For better comprehension of this case, take in the progressions of sending email utilizing JavaMail API first.


For sending the email utilizing JavaMail API, you have to stack the two container records:

  • mail.jar
  • activation.jar

download these jar files go to the Oracle site to download the latest version.


Ventures for accepting the email utilizing JavaMail API

There are 5 stages to get the email utilizing JavaMail API. They are:

  1. Get the session object
  2. make the POP3 store protest and unite with the pop server
  3. create the folder object and open it
  4. recover the messages from the folder in a array and print it
  5. close the store and folder objects

Sample of Receiving email utilizing JavaMail API

We should see the illustration of accepting the mail utilizing java mail programming interface.

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 ReceiveMail{  
  
 public static void receiveEmail(String pop3Host, String storeType,  
  String user, String password) {  
  try {  
   //1. get the session object  
   Properties properties = new Properties();  
   properties.put("mail.pop3.host", pop3Host);  
   Session emailSession = Session.getDefaultInstance(properties);  
     
   //2. make the POP3 store protest and unite with the pop server  
   POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);  
   emailStore.connect(user, password);  
  
   //3. create the folder object and open it  
   Folder emailFolder = emailStore.getFolder("INBOX");  
   emailFolder.open(Folder.READ_ONLY);  
  
   //4. recover the messages from the folder in	a array and print it  
   Message[] messages = emailFolder.getMessages();  
   for (int i = 0; i < messages.length; i++) {  
    Message message = messages[i];  
    System.out.println("---------------------------------");  
    System.out.println("Email Number " + (i + 1));  
    System.out.println("Subject: " + message.getSubject());  
    System.out.println("From: " + message.getFrom()[0]);  
    System.out.println("Text: " + message.getContent().toString());  
   }  
  
   //5. close the store and folder objects  
   emailFolder.close(false);  
   emailStore.close();  
  
  } catch (NoSuchProviderException e) {e.printStackTrace();}   
  catch (MessagingException e) {e.printStackTrace();}  
  catch (IOException e) {e.printStackTrace();}  
 }  
  
 public static void main(String[] args) {  
  
  String host = "mail.javatportal.com";//change appropriately  
  String mailStoreType = "pop3";  
  String username= "mithileshgniit@javatportal.com";  
  String password= "******";//change appropriately 
  
  receiveEmail(host, mailStoreType, username, password);  
  
 }  
}  

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