Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Receiving Email


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 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. public class ReceiveMail{
  10.  public static void receiveEmail(String pop3Host, String storeType,
  11.   String user, String password) {
  12.   try {
  13.    //1) get the session object
  14.    Properties properties = new Properties();
  15.    properties.put(“mail.pop3.host”, pop3Host);
  16.    Session emailSession = Session.getDefaultInstance(properties);
  17.    //2) create the POP3 store object and connect with the pop server
  18.    POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
  19.    emailStore.connect(user, password);
  20.    //3) create the folder object and open it
  21.    Folder emailFolder = emailStore.getFolder(“INBOX”);
  22.    emailFolder.open(Folder.READ_ONLY);
  23.    //4) retrieve the messages from the folder in an array and print it
  24.    Message[] messages = emailFolder.getMessages();
  25.    for (int i = 0; i < messages.length; i++) {
  26.     Message message = messages[i];
  27.     System.out.println(“———————————“);
  28.     System.out.println(“Email Number “ + (i + 1));
  29.     System.out.println(“Subject: “ + message.getSubject());
  30.     System.out.println(“From: “ + message.getFrom()[0]);
  31.     System.out.println(“Text: “ + message.getContent().toString());
  32.    }
  33.    //5) close the store and folder objects
  34.    emailFolder.close(false);
  35.    emailStore.close();
  36.   } catch (NoSuchProviderException e) {e.printStackTrace();}
  37.   catch (MessagingException e) {e.printStackTrace();}
  38.   catch (IOException e) {e.printStackTrace();}
  39.  }
  40.  public static void main(String[] args) {
  41.   String host = “mail.Kreationnext.com”;//change accordingly
  42.   String mailStoreType = “pop3”;
  43.   String username= “sonoojaiswal@Kreationnext.com”;
  44.   String password= “xxxxx”;//change accordingly
  45.   receiveEmail(host, mailStoreType, username, password);
  46.  }
  47. }
download this example to receive email

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

Comments

comments