In this article I would like to show some example of how to check email boxes in automation testing using Java instruments.
So, let’s think about how many times You had to develop test scenarios of user registration, activation using Selenium or Appium. And in 90% it’s very important to verify e-mail box that welcome message is received or something like this. Usually engineers use services like Mailinator or similar to check the mailbox. But such services are not free. And not secure.

I will show how to check even private Gmail box in automation testing using IMAP protocol. The main instrument is native java library javax.mail.*

1. Implement login

public void login(String host, int port, String username, String password) throws Exception {
            String fName = getFolderName(folderName);
            URLName url = new URLName(protocol, host, port, fName, username, password);

            if (session == null) {
                        Properties props = null;
                        try {
                                   props = System.getProperties();
                        } catch (SecurityException sex) {
                                   props = new Properties();
                        session = Session.getInstance(props, null);
             store = session.getStore(url);
             folder = store.getFolder(url);

2. Get messages

public Message[] getMessages() throws MessagingException {
             return folder.getMessages();

Also it’s possible to verify if user is connected, possible to logout from the session.
The wrapper for using of such mail services  is implemented in Automotion library

The methods that could be accessible from the code are:

MailService mailService = new MailService();
           .setFolder(MailService.MailFolder.INBOX) // (INBOX, SPAM, TRASH)
           .login(String IMAP_Server, int IMAP_Port, String email, String passwordToEmail);

mailService.isLoggedIn()) - boolean
mailService.getMessageCount() - integer
mailService.getMessages() - Message[]
mailService.getLastMessage() - Message - last message

Now You know how to perform the verification that email was successfully sent and received.

Thanks for reading my articles! Have a good automation!