How to Automate Email Verification

Automate Email Verification

Email verification is one of the common areas which remain in the not automatable list of any regression suite. Here we will talk about how to automate email verification using Java mail client. Once we have the script to wait for and access the mail, we can extend it to automate scenarios for setting up new accounts which require email verification.

We have two approaches.

  1. Access the email via webmail and perform the actions using selenium
  2. Access the email using Java mail client

We will be discussing the verification using Java mail client as the previous one is pretty simple. You can download the javax mail jar from here.

Automate Email Verification with Java Mail Client

We have two areas to verify

  • Whether email is received or not
  • Subject, headers and body of email

We have to do the following things

  • Get the email with the particular subject or sender
  • Open the email and validate the details
  • Mark the mail as read or delete it

Get all email messages

There are two types of email protocols – POP3 and IMAP. We will be covering both separately.

Open the required Email

After getting the emails, we have to find the email that we are looking for. This can be done using either the subject or any other field in the email. Here we find the email using the subject as shown below.

SEE ALSO:   Test Responsive Design using Galen Framework

Note that for IMAP messages, message.getContent() will mark it as Read. Other way of doing it is by setting the SEEN flag.

You can see a sample usage of the above methods here.


Here we have seen getting all the emails and checking for the required one by comparing the subject of each. For this to work, we have to wait for a specific time so that we are sure that the email is already received before we get all the messages. In certain cases, we may have to read all the emails in a loop in case we don’t find the required email.

You can find the completed class in this github project.

Further Reading:

You may also like...

1 Response

  1. Appreciate this post. Will try it out.

Leave a Reply

Your email address will not be published. Required fields are marked *