Home » Java Tutorial » Reading e-mail using Javamail api

Reading e-mail using Javamail api

In this post we are going to see how to read e-mail using Javamail api. Like in a complete and robust automation test framework, it is very important if the automation script is smart enough to read, manipulate and send mails to appropriate stakeholders. I think discussing scenarios when we require manipulating or sending mails is not important here but how we can read, verify text from mail, clicking on a link in the mail, send mails is more important to discuss and this has been done possible with the help of javamail api. So Let’s discuss  about actions that can be performed easily with javamail api:

  1. Read Mail.
  2. Verify the subject of the mail
  3. Verify any specific content in the mail text.
  4. Click on a particular link in the mail body
  5. Send mails to people

Don’t be bothered this all can be done in java in just a very simple way but for the same you need to go through this complete post where we will start from a very basic and will see gradually how things are done with the help of javamail api.

[the_ad_placement id=”incontent”]

First of all we need to have the ‘.jar’ file to be added in the project. JAR file can be downloaded form ‘http://java.net/projects/javamail/downloads/download/javax.mail.jar’ or if you use Maven then you need to add following dependencies to keep all the required jar file in build path:

JavaMail API provides a platform-independent solution for mail messaging applications. It defines a set of classes that comprise a mail system. It is an optional package (standard extension) for reading, composing, and sending e-mails.

Read Mail Using JavaMail API:

In order to read mail we need to have an email to be read. Please take a look at below code snippet:

String username = [email protected];

String password = “Password1”;

String hostName = “mail.pqr.com”;

int messageCount;

In the above code snippet we saved basic details like username,password and hostname into the string variable. Now what is ‘mail.pqr.com’ that we saved in host name. This is the server DNS name which is serving as email server. Email server does the job of storing all the mails for a particular domain. We have to connect to this server to read the mails.

There are some protocols that are used in JavaMail API.

  • SMTP(Simple Mail Transfer Protocol)
  • POP(Post Office Protocol)
  • IMAP(Internet Message access Protocol)
  • MIME(Multiple Internet Mail Extension)
  • Others: Network News Transfer Protocol (NNTP)

Details of above protocols that how they work is not the point to talk here. But a point to take a note is that SMTP is used to send mail and POP, IMAP are used to read mails.

The above code will help you out to read the mail using Java Mail API. Let’s take a closer look of above LOC.

This is essential property and needs to get set. It defines the protocol using which we are going to read mail. Protocols can be either imaps or POP. You may or may not want to set following properties. Please note that these are not required but you may need it based on your situation.

[the_ad_placement id=”incontent”]

Create Session:

Now we need to get/book a session for this communication. The session acts as a connection setting between JavaMail API and Mail Server. It can be done using below code:

Get Mail store:

Store Class acts as a Store to put the messages that we have in the mail server. Let’s think of a departmental store shop where you can find your daily households. Things are segregated using compartmental or rack system manner. Same way Store Class works. It stores all of the messages. You might need to ask which folder you want to see. This can be achieved via below code:

Access a Mail Folder:


Folder class lets you get the messages from a particular folder. Like:

In the above example code Inbox folder of a mail is opened in Read Only mode. One can open this as Read_Write mode also using:

Now message count from the mail box can be retrieved by ‘getMessageCount’ method. All the unread message count can be get using ‘getUnreadMessageCount’.


Read Email Messages:

Message class helps us to get one particular message as object.

If you want to search message from any defined subject, this can be done as below:

getContent() method will help you out to get the content the an e-mail.

Above LOC will return the mail content as string which can be manipulated in any way as you want like getting the hyper-link text, presence of a particular word etc.

Close the mail box: Please do me a favour folks, close the doors once you are done also do not forget to shut your mailbox using below snippet:

Read Similar Posts

3 thoughts on “Reading e-mail using Javamail api”

  1. Finally after a long time abodeqa is back. Thanks Namal Singhal for such a nice article. I have read it somewhere regarding Gmail API to test email validatation and I tried as well but was not successful. Can you provide some good example on Gmail API or some other API to test email validation.

  2. Pingback: Testing Bits – 4/26/15 – 5/2/15 | Testing Curator Blog

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: