How to send a mail in gmail using selenium java

To send an email using Selenium WebDriver in Java, you'll need to use a library that can interact with Gmail's web interface. One popular option is the gmail-java library, which provides a simple API for sending emails.

Here's a step-by-step guide on how to send an email using Selenium WebDriver in Java:

Prerequisites:

  1. Install Selenium WebDriver for Java (e.g., selenium-java-4.0.0.jar)
  2. Install the gmail-java library (e.g., gmail-java-1.0.0.jar)
  3. Create a Gmail account and enable "Less secure app access" in your account settings (Settings > Accounts > Security > Less secure app access)

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import com.gmail.java.*;

public class SendEmailUsingSelenium {
    public static void main(String[] args) {
        // Set up ChromeDriver
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        // Navigate to Gmail login page
        driver.get("https://mail.google.com");

        // Enter email address and password
        WebElement emailInput = driver.findElement(By.id("Email"));
        emailInput.sendKeys("[email protected]");
        WebElement passwordInput = driver.findElement(By.id("Passwd"));
        passwordInput.sendKeys("your-password");

        // Click the login button
        WebElement loginButton = driver.findElement(By.id("signIn"));
        loginButton.click();

        // Wait for the inbox page to load
        driver.wait(5000);

        // Compose a new email
        WebElement composeButton = driver.findElement(By.cssSelector("#:nth-child(2) > a"));
        composeButton.click();

        // Enter the recipient's email address
        WebElement toInput = driver.findElement(By.id("to"));
        toInput.sendKeys("[email protected]");

        // Enter the subject
        WebElement subjectInput = driver.findElement(By.id("subjectbox"));
        subjectInput.sendKeys("Test email from Selenium");

        // Enter the email body
        WebElement bodyInput = driver.findElement(By.id("body"));
        bodyInput.sendKeys("This is a test email sent using Selenium WebDriver.");

        // Click the send button
        WebElement sendButton = driver.findElement(By.id("sendmail"));
        sendButton.click();

        // Close the browser
        driver.quit();
    }
}

Note:

  1. Replace [email protected] and your-password with your actual Gmail credentials.
  2. Replace [email protected] with the recipient's email address.
  3. Make sure to install the gmail-java library and add it to your project's classpath.
  4. This code uses the ChromeDriver, but you can use other browsers like Firefox or Edge by changing the WebDriver implementation.

Limitations:

  1. This code uses the Gmail web interface, which means it's subject to the same limitations as a regular web browser.
  2. You may need to handle CAPTCHAs or other security measures that Gmail implements to prevent automated login attempts.
  3. This code is for demonstration purposes only and should not be used for production email sending.