Skip to main content

Verify email confirmation using Selenium WebDriver

Note: If you are new to java and selenium then start with selenium java training videos. 



How to Verify Email Confirmation Using Selenium 4 and JavaMail (2026 Guide)

Email confirmation is a critical part of most registration flows — account activation, password reset, multi-factor authentication, and onboarding.

Every automation engineer eventually faces the same challenge: How do you verify an email confirmation link inside a Selenium test without making it slow and flaky?

The wrong instinct is to automate Gmail's UI with Selenium. It's fragile, slow, and breaks constantly. The right approach:

  1. Use Selenium for browser automation
  2. Use JavaMail (IMAP) to read the email directly
  3. Extract the confirmation link
  4. Continue the test in Selenium

Why Not Automate Gmail UI With Selenium?

Automating the Gmail UI means logging in, searching, clicking a message, and parsing content from a third-party interface that changes frequently. This leads to:

  • Flaky, slow tests
  • Tight coupling to an external UI
  • Authentication headaches (2FA, CAPTCHAs)

Reading the mailbox directly via IMAP is cleaner, faster, and more reliable.

Modern Requirements

⚠️ Gmail no longer supports simple username/password authentication for IMAP.

You must:

  1. Enable 2-Step Verification on your Gmail account
  2. Generate a Gmail App Password
  3. Use that App Password for IMAP access
  4. Store credentials in environment variables or CI secrets — never in code

Maven Dependencies

<!-- JavaMail -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>jakarta.mail</artifactId>
    <version>2.0.1</version>
</dependency>

<!-- HTML email parsing -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.17.2</version>
</dependency>

Step 1: Email Helper Class (IMAP Polling)

import jakarta.mail.*;
import jakarta.mail.search.SubjectTerm;
import java.util.Properties;

public class EmailHelper {

    public static Message waitForEmail(String subject, int timeoutSeconds) throws Exception {
        String host = "imap.gmail.com";
        String username = System.getenv("TEST_EMAIL_USER");
        String password = System.getenv("TEST_EMAIL_PASS");

        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imaps");

        Session session = Session.getInstance(properties);
        Store store = session.getStore("imaps");
        store.connect(host, username, password);

        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);

        int waited = 0;
        while (waited < timeoutSeconds) {
            Message[] messages = inbox.search(new SubjectTerm(subject));
            if (messages.length > 0) {
                return messages[messages.length - 1];
            }
            Thread.sleep(5000);
            waited += 5;
        }

        throw new RuntimeException("No email found with subject: " + subject);
    }
}

Key design decisions:

  • Polls every 5 seconds rather than using a hard-coded sleep
  • Returns the latest matching message to avoid stale results
  • Credentials loaded from environment variables

Step 2: Extract the Confirmation Link

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class EmailParser {

    public static String extractConfirmationLink(Message message) throws Exception {
        String content = message.getContent().toString();
        Document document = Jsoup.parse(content);
        Element link = document.select("a").first();

        if (link == null) {
            throw new RuntimeException("No confirmation link found in email.");
        }

        return link.attr("href");
    }
}

💡 Tip: If your email contains multiple links, use a more specific CSS selector — e.g., a[href*="confirm"] — to avoid grabbing the wrong one.

Step 3: Wire It Together in Selenium 4

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class EmailVerificationTest {

    public static void main(String[] args) throws Exception {
        Message message = EmailHelper.waitForEmail("Welcome to My Application", 60);
        String confirmationUrl = EmailParser.extractConfirmationLink(message);

        WebDriver driver = new ChromeDriver();
        driver.get(confirmationUrl);

        System.out.println("Registration page loaded: " + driver.getTitle());
        driver.quit();
    }
}

The full test flow:

  1. Register user
  2. Poll inbox for confirmation email
  3. Extract link
  4. Open link in browser
  5. Continue validation

Handling Email Delivery Delays

Do: Poll with a timeout, fail with a clear error message, search by subject or unique token, delete/mark email after use.

Avoid: Thread.sleep(30000), silent failures, grabbing the first available email, leaving stale emails that cause false positives.

CI/CD Considerations

  • Store email credentials as pipeline secrets
  • Use a dedicated test mailbox — not a personal account
  • Clean the inbox regularly to avoid false positives from old emails
  • Scope searches by date to avoid matching stale messages

Better Alternative: Fake SMTP Servers

For non-production environments, skip Gmail entirely and use a local mail capture tool:

  • MailHog — great for local dev and Docker setups
  • GreenMail — integrates directly with JUnit tests

These capture outgoing emails with no external dependency — the preferred approach for enterprise test automation.

Summary

Wrong WayRight Way
Automate Gmail UIRead inbox via IMAP
Hard-coded sleepsPoll with timeout
Hardcoded credentialsEnvironment variables
String splittingJsoup HTML parsing
Coupled email + Selenium logicSeparate helper classes

Following this pattern keeps your automation framework clean, reliable, and easy to maintain.

Comments

  1. Great post, thanks for sharing. Also maybe we could get a link to a straight text file as the blogger doesn't allow for us to copy the code you posted to test it out.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. You should be able to copy now, let me know if you are not

    ReplyDelete
  4. Thanks to Steve for identifying a problem with code, I have corrected it now

    ReplyDelete
  5. To be honest I do not agree. We use gmail box to store our confirmation emails. We have a class that lets us log in to gmail and access the last message, which you can then parse to find links, texts, images, etc. It's a simple class, just a few lines of code so I wouldn't say it's not clean. Moreover in this way we don't need any custom reporting (we can use our default reporting system for Selenium).

    However I can see one advantage of your solution. If you use POP3 then it's much simpler to switch between mailboxes (if one day you decide you cannot use gmail for any reason). Also this way probably is a little bit faster too.

    ReplyDelete
  6. Interesting post. Thank you!

    ReplyDelete
  7. Not able to copy the code... :(

    ReplyDelete
    Replies
    1. You can use ViewRaw link to copy code or use this - https://gist.github.com/1558421#file_seemailverify.java

      Delete
  8. This is really a great post.
    But when I need to check mails, I do not have username and password. Just need to verify if email workflow is being fired or not through SharePoint. Can you please help?

    ReplyDelete
    Replies
    1. In that case you may have to check on your SMTP server about email being fired

      Delete
  9. hi,
    is there any way to assert mail template using selenium?

    ReplyDelete
    Replies
    1. Could you elaborate more on your question?

      Delete

Post a Comment

No spam only genuine comments :)

Popular posts from this blog

Selenium Tutorial: Ant Build for Selenium Java project

Ant is a build tool which could be used to have your tests running either from command line or from Hudson CI tool. There is detailed documentation available for ant here but probably you need to know only a little part of it for you selenium tests. The essentials which are needed to know are: Project Target (ant execution point and collection of tasks) Tasks (could be as simple as compilation) And there would usually be following targets for Selenium tools - setClassPath - so that ant knows where you jar files are loadTestNG - so that you could use testng task in ant and use it to execute testng tests from ant init - created the build file clean - delete the build file compile - compiles the selenium tests run - executes the selenium tests Here is my project set up for ant -

Using xPath to reach parent of an element

Note: If you are new to java and selenium then start with selenium java training videos .   I prefer css locator over xPath but there are times when css locators don't fit requirement. One such requirement is when you want to navigate to parent element of an element and may be parent of parent and even more. Unfortunately css locators don't provide any mechanism to navigate to parent of an element. See this for more. Of late I came across a scenario when I wanted to click on a link depending upon the text in a text box. Herein parent of text box and parent of link were at the same location. More over there could have been many such combinations in application. Fortunately I just need to pick first such instance and Web Driver any way considers only first instance when multiple locators are found matching an element. Element in question is in following html - Here I need to click on highlighted anchor on the basis of input element (which is also hig...