I have multiple email templates in my regression pack and tests include validating the content of email received (examples: Email address, name etc.).

Does anyone have an idea on how to automate the process of validating the content of email templates ?

  • Can you add some description? Jan 17, 2016 at 17:36
  • Welcome to SQA! It would help getting good answers if you add details like the environment and such. It impacts the approach, for example different tools would apply if you work with a web mail client or a native Windows or Linux mail client. Also I'm not sure how templates and received mails would relate?
    – Bookeater
    Jan 17, 2016 at 23:31

1 Answer 1


Yes, you can validate the content of email.

If you need to write selenium script which will trigger the email, then you can use javax mail API for fetching the email from inbox.

Following is code for fetching the email from inbox.

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;

Above function will return you the message, then you can perform operations on message object, for example reading mail subject you can use following code.


Following code to get details of FROM


And many more functions.

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.