There are two aspects to which needs to understood before proceeding with this chapter. They are Check and Fetch.
Checking an email in JavaMail is a process where we open the respective folder in the mailbox and get each message. Here we only check the header of each message i.e the From, To, subject. Content is not read.
Fetching an email in JavaMail is a process where we open the respective folder in the mailbox and get each message. Alongwith the header we also read the content by recognizing the content-type.
To check or fetch an email using JavaMail API, we would need POP or IMAP servers. To check and fetch the emails, Folder and Store classes are needed. Here we have used GMAIL's POP3 server (pop.gmail.com). In this chapter will learn how to check emails using JavaMail API. Fetching shall be covered in the subsequent chapters. To check emails:
Get a Session
Create pop3 Store object and connect with pop server.
Create folder object. Open the appropriate folder in your mailbox.
Get your messages.
Close the Store and Folder objects.
Create a java class file CheckingMails, the contents of which are as follows:
package com.howcodex; 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 javax.mail.Store; public class CheckingMails { public static void check(String host, String storeType, String user, String password) { try { //create properties field Properties properties = new Properties(); properties.put("mail.pop3.host", host); properties.put("mail.pop3.port", "995"); properties.put("mail.pop3.starttls.enable", "true"); Session emailSession = Session.getDefaultInstance(properties); //create the POP3 store object and connect with the pop server Store store = emailSession.getStore("pop3s"); store.connect(host, user, password); //create the folder object and open it Folder emailFolder = store.getFolder("INBOX"); emailFolder.open(Folder.READ_ONLY); // retrieve the messages from the folder in an array and print it Message[] messages = emailFolder.getMessages(); System.out.println("messages.length---" + messages.length); for (int i = 0, n = messages.length; i < n; 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()); } //close the store and folder objects emailFolder.close(false); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String host = "pop.gmail.com";// change accordingly String mailStoreType = "pop3"; String username = "yourmail@gmail.com";// change accordingly String password = "*****";// change accordingly check(host, mailStoreType, username, password); } }
Now that our class is ready, let us compile the above class. I've saved the class CheckingMails.java to directory : /home/manisha/JavaMailAPIExercise. We would need the jars javax.mail.jar and activation.jar in the classpath. Execute the command below to compile the class (both the jars are placed in /home/manisha/ directory) from command prompt:
javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: CheckingMails.java
Now that the class is compiled, execute the below command to run:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: CheckingMails
You should see the following message on the command console:
messages.length---4 --------------------------------- Email Number 1 Subject: Test Mail--Fetch From: <abcd@gmail.com> Text: javax.mail.internet.MimeMultipart@327a5b7f --------------------------------- Email Number 2 Subject: testing ----checking simple email From: <abcd@gmail.com> Text: javax.mail.internet.MimeMultipart@7f0d08bc --------------------------------- Email Number 3 Subject: Email with attachment From: <abcd@gmail.com> Text: javax.mail.internet.MimeMultipart@30b8afce --------------------------------- Email Number 4 Subject: Email with Inline image From: <abcd@gmail.com> Text: javax.mail.internet.MimeMultipart@2d1e165f
Here we have printed the number of messages in the INBOX which is 4 in this case. We have also printed Subject, From address and Text for each email message.