In my application I am supposed to test/fetch the data and contents of the E-Mail received after triggering the Schedule. How can I automate this scenario using Selenium web-driver ?


1 Answer 1


To automate Email, you can use javax mail API for fetching the email from the inbox or any other folder.

Following is the function for fetching an email.

private Message getEmail(String emailID, String password, String subjectToBeSearched) throws Exception {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", emailID, password);

Folder folder = store.getFolder("INBOX");

Message[] messages = null;
boolean mailFound = false;
Message email = null;

for (int i = 0; i < 30; i++) {
    messages = folder.search(new SubjectTerm(subjectToBeSearched), folder.getMessages());
    if (messages.length == 0) {

for (Message mail : messages) {
    if (!mail.isSet(Flags.Flag.SEEN)) {
        email = mail;
        mailFound = true;

if (!mailFound) {
    throw new Exception("Could not found Email");

return email;

You can call this function by passing email Id and Password, It will return you an email.

Then you can perform various operation on the return object, for example:

public verifyEmail() throws Exception {
Message email = getEmail(emailID, password, "Subject of Email to be 


// and many more functions

Following is the link to download the javax mail API


or you can use maven dependency


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.