3

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 ?

2

1 Answer 1

4

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");
folder.open(Folder.READ_WRITE);

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) {
        Thread.sleep(10000);
    }
}

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 

searched");
System.out.println(email.getAllRecipients());
System.out.println(email.getContent());
System.out.println(email.getSubject());

// and many more functions
    }

Following is the link to download the javax mail API

http://mvnrepository.com/artifact/javax.mail/mail/1.4.7

or you can use maven dependency

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</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.