Email Verification with Selenium
Many applications may send verification emails and get first time password from emails. Often, tests related to this are left to manual testing, instead of putting them into an automated test suite.
However, there’s no need to check emails manually: it suffers from all the same problems as another manual testing. It’s slow and expensive. There are many libraries available to interact with email through code — this post will focus on how to use them within an automated test suite.
Getting started
The tests are automatically configured to use Gmail, with a fake testing account we have set up for display purposes.
If using Gmail for your test email account, you may face some problems with authentication and need to allow less secure apps to access your account.
Scenario :
Launch Gmail using Selenium;
Somehow search for new mail in the list of available mails;
Somehow to click on it;
Parse the mail message;
Get the password;
Follow up with the registration process
The JavaMail jar files are published to the Maven repository. The main JavaMail jar file, which is all most applications will need, can be included using this Maven dependency
We need to add maven dependencies in pom.xml file
That is the example of email automatically verify and get a password from it.
we have the email verification class now we need to just call in our test case so lets code for getting the password from the email account.
Thank you for reading, if you have anything to add please send a response or add a note!
Reading/Sending email by Selenium
The JavaMail API defines classes which represent the components of a mail system. JavaMail does not implement an email server, instead it allows you to access an email server using a Java API. In order to test the code presented, you must have access to an email server. While the JavaMail API specification does not mandate support for specific protocols, JavaMail typically includes support for POP3, IMAP, and SMTP.
Prerequisite:
- Have access to an SMTP server. You must know the host name, port number, and security settings for your SMTP server. Web mail providers may offer SMTP access, view your email account settings or help to find further information. Be aware that your username is often your full email address and not just the name that comes before the @ symbol.
- A Java EE IDE and Application Server such as GlassFish or Oracle WebLogic Server. JavaMail can be downloaded as a library in a Java SE application but this tutorial assumes the use of a Java EE application server which would already include JavaMail.
Steps for sending email:
We will cover below 3 scenario to send an email:
1. Send Mail in Java using SMTP without authentication.
2. Send Mail in Java using SMTP with TLS authentication.
3. Send Mail in Java using SMTP with SSL authentication
1.Download java mail jar file which contains the library to send the email.

2.Add jar into your project and if you are working with maven project then you can use dependency.
3. Get the session object – . javax.mail.Session class provides object of session, Session.getDefaultInstance() method and Session.getInstance() method.
Compose the message–
javax.mail.Transport class provides method to send the message.
Send the message –
1.Send Mail in Java using SMTP without authentication full implementation in java
The program is simple to understand and works well, but in real life most of the SMTP servers use some sort of authentication such as TLS or SSL authentication. So, we will now see how to create Session object for these authentication protocols.
For TLS & SSL you can to know port in which the mail server running those service.
2. Following is the Send Mail in Java using SMTP with TLS authentication full implementation :
3. Following is the Send Mail in Java using SMTP with SSL authentication full implementation :
How to test emails with Selenium
It’s easy to forget to include email testing in your test automation workflow.
I’m going to show you how to test emails.
Emails are important.
Most applications send automatic emails to users.
For example, you get an email when you sign up on a website and you usually need to click on the confirmation link from that email.
You might also get transactional emails when you perform certain actions on a service.
Testing emails is important.
Like any responsible developer, you probably have some automated tests that verify if your web application is working as expected.
You should also verify if the emails sent by your web application look good and work as expected.
Think of this nightmare scenario:
Something breaks in your app and the email confirmation no longer works for users who want to sign up.
This is a preventable type of disaster.
Emails are basically just HTML and CSS.
This makes it easy to test them with Selenium or any other testing tool.
And not just visually.
We can easily click on links and other elements from the email.
The wrong way.
It would be difficult to rely on a single email address.
Even if we would have steps to delete that email address from our application, it would still be inconvenient.
And we cannot use our work email or our Gmail account. Since they tend to have 2FA, reCAPTCHAs and other tricky obstacles.
The right way.
We can use a disposable email service, as long it’s reliable and secure.
The smart way.
We built a free email testing service called Endtest Mailbox.
Anyone can use it and you don’t have to sign up.
But how does it work?
The way it works is similar to a disposable email service.
Simply send your email(s) to a username@endtest-mail.io email address.
The username can be anything you choose and you don’t have to register it anywhere.
For instance, you can choose something like jim32328@endtest-mail.io.
You can access the Inbox for that email address on this page.
Just add the email parameter in the URL, like this:
Let’s see an example.
Let’s write some Python + Selenium code that will perform a sign up on a website.
Exit fullscreen mode
And that code would lead us to this screen:

And we’ll be able to open our email:

And you can use any testing tool, browser or mobile device to access it.
Here is how you would access it with Python + Selenium:
Exit fullscreen mode
More details are available in the How to test Emails chapter from our Documentation.
Selenium WebDriver – EMail Validation With Disposable EMail Addresses
Some of our automated tests might require email validation. Your application might send a Welcome email / User registration confirmation with a link to activate the profile / a token to login as part of Multi-factor Authentication…etc. Your aim could be to check if the email has received or extract some information like auth token from the email message.
Sometimes, applications might expect unique email addresses. In that case, we end up using some random email addresses like random-string@gmail.com. Of-course this is not a valid email address. Your application does not care as long as it is in the expected format & not used already. However, if your application tries to send any email as part of your tests for that random email, How will you verify?
Would it not be legendaryyyyyyyyyyyyyyyy as Barney would say if we could have an API which generates a Valid & Random email address every time through which you could receive emails?

Lets see how we could achieve that!
Sample Application:
As usual, to explain things better, I consider this site to send an email. When you enter an email id and click on check, a test email is sent to the given email id immediately. Lets consider this as our application and we need to verify if it sends an email correctly to the given email address!

We create a Page Object for the above page as shown here.
Nada – EMail API:
To test our sample website, we want a random working email. So, I am considering this site which uses some API internally. Nada website creates random working email addresses. No worries. We will not be using the web interface. We would use its API which the web interface uses.

Inbox API:
Nada has few email domains (ex: nada.ltd). Using that you could request for any email address!Making a GET request with an email address creates an instant Inbox for the email address.
here
- helloworld@nada.ltd
- thisisjunk1234@nada.ltd
It does not require any password. So, If i am using helloworld@nada.ltd and you are also using it at the same time, you could see my emails. So, do not use for any sensitive information. This is just for testing purposes. msgs field in the below json is the email inbox which indicates inbox is empty.

When I enter this email address in the ‘IsMyEmailWorking’ site, I get an email as shown here.

Message API:
Inbox API gives all the list of email messages we have received for the email. If we need to get the email content, we need to get the ‘uid’ of the specific email message, then call this API.
Here

EMail Service:
We have seen the APIs. So, Lets create few classes to make the HTTP calls & process the response JSON etc.
My pom file has below dependencies.
Inbox EMail:
We need to convert Nada’s inbox API response JSON to an object. So, I create this class.
Message Class:
To Process message API response,
NadaEMailService:
Our API exposes 5 methods.
- getEmailId – generates a random email. Once generated for an instance, it is always reusing the same email. If you want a new one, call reset method
- reset – nullifies the email id. so that we could re-generate new random email id
- getInbox – list of inbox messages
- getMessageById – pass the id of the message to get the content
- getMessageWithSubjectStartsWith – pass the email subject to get the latest message
Sample Test:
Summary:
Nada API is a great utility for email validation. By using Nada API, you could create N number of random email id as and when it is required & dispose. It would be extremely useful for for your tests which require unique email addresses every run!
Nada is FREE. You could help Nada’s maker by buying him a coffee here .