How to automate email validation?

Sometimes we are required to automate a test that asks the user to validate email. e.g. on registering a new user, a welcome email is sent to the user’s registered email address. One simple way is to open the webmail in the browser and automate it like any other web application. However, it has some limitations like serious security concerns, increased execution time etc.

For this tutorial, we are taking an example of Gmail. The similar steps would work for other providers.

We can use the IMAP protocol to hook to a Gmail account in the background, query emails and perform validations. We can do all sort of basic actions like reading/sending email without opening it in the browser.

The below example is in Java. However, you can easily convert it into other languages. I would strongly encourage you to post your implementations in comments.

  • First download JavaMail API – javax.mail.jar provided by Java from here.
  • Include it in your project’s build path. In case you missed how to do that please take reference from here.
  • Here is the complete code. Please feel free to make changes to suit your requirements-
import javax.mail.*;
import java.util.Properties;

public class GMail {
	//Author: Shadab Ansari,
	//Method to read emails from GMail without opening GUI by using IMAP protocol.
	//Modify code accordingly as per your requirements
	//First download JavaMail API -
	//Include javax.mail.jar in Project's build path
	public void automateGmailUsingImap(final String USERNAME, final String PASSWORD) {
	 Folder folder = null;
	    Store store = null;
	    System.out.println("***Reading mailbox...");
	    try {
	      Properties props = new Properties();
	      props.put("", "imaps");
	      Session session = Session.getInstance(props);
	      store = session.getStore("imaps");
	      store.connect("", USERNAME, PASSWORD);
	      folder = store.getFolder("INBOX");;
	      Message[] messages = folder.getMessages();
	      System.out.println("No of Messages : " + folder.getMessageCount());
	      System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
	      for (int i=0; i < messages.length-1; i++) {
	        System.out.println("Reading MESSAGE # " + (i + 1) + "...");
	        Message msg = messages[i];
	        String strMailSubject ="", strMailBody ="";
	        //Getting mail subject
	        Object subject = msg.getSubject();
	        //Getting mail body
	        Object content = msg.getContent();
	        //Casting objects of mail subject and body into String
	        strMailSubject = (String)subject;
	        strMailBody = (String)content;
	        //Printing mail subject and body
	        //For debugging get only 10 emails
	        // break;
	    }catch(MessagingException messagingException){
	    }catch(IOException ioException){
	    }finally {
	      if (folder != null) {
	    	  try {
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
	      if (store != null) {
	    	  try {
			} catch (MessagingException e) {
				// TODO Auto-generated catch block


Leave a Reply