Sending email report with attachments in Selenium Webdriver

When we are using Selenium or any other automation tool, we are performing operations on the web application. But our purpose of automation is not just to exercise the Application Under Test. We, as an automation tester are supposed to test the application, find bugs and report it to the development team. In this article , we are going to see how we can send reports through email in Selenium Webdriver. 

Get Started

Add Maven dependency in pom.xml file

How to send report through email using Selenium Webdriver

The method inside which all the code is written so that we can invoke it easily from any class.

public void emailSend(String text) throws MessagingException {}

1. Set below properties to send mail using Gmail

// Create object of Property
Properties props = new Properties();

// this will set host of server- you can change based on your requirement
props.put(“mail.smtp.host”, “smtp.gmail.com”);

// set the port of socket factory
props.put(“mail.smtp.socketFactory.port”, “465”);

// set socket factory
props.put(“mail.smtp.socketFactory.class”,“javax.net.ssl.SSLSocketFactory”);

// set the authentication to true
props.put(“mail.smtp.auth”, “true”);

// set the port of SMTP server
props.put(“mail.smtp.port”, “465”);

2.  Get the session object and pass your email account’s credentials.

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“emailid@gmail.com”, “Password”);
}
});

3. Specify the “from” and “to” address and subject line

// Create object of MimeMessage class
Message message = new MimeMessage(session);

// Add the subject link
message.setSubject(“Testing Subject”);

// Set the from address
message.setFrom(new InternetAddress(“from”));

// Set the recipient address
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(“to”));

4. Set the mail body

// Create object to add multimedia type content
BodyPart messageBodyPart1 = new MimeBodyPart();

// Set the body of email
messageBodyPart1.setText(“This is mail body”);

5.  Add attachment in mail body

// Create another object to add another content
MimeBodyPart messageBodyPart2 = new MimeBodyPart();

// Mention the file which you want to send
String filename = System.getProperty(“user.dir”) + “filename”;

// Create data source and pass the filename
DataSource source = new FileDataSource(filename);

// set the handler
messageBodyPart2.setDataHandler(new DataHandler(source));

// set the file
messageBodyPart2.setFileName(filename);

6. Send the email

// Create object of MimeMultipart class
Multipart multipart = new MimeMultipart();

// add body part 1
multipart.addBodyPart(messageBodyPart2);

// add body part 2
multipart.addBodyPart(messageBodyPart1);

// set the content
message.setContent(multipart);

// finally send the email
Transport.send(message);

Code

@AfterTest
void emailSend(String error){
   
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
              "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
       new javax.mail.Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication("XXXXXX...@gmail.com", 
       "XXXXXXXXX");
             } });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new 
        InternetAddress("XXXXXX...@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("XXXXXX...@gmail.com"));
        message.setSubject("Testing Subject");

        BodyPart messageBodyPart1 = new MimeBodyPart();

        messageBodyPart1.setText(error);

        MimeBodyPart messageBodyPart2 = new MimeBodyPart();

        String filename = System.getProperty("user.dir") +
        "/screenshot.jpg";

        DataSource source = new FileDataSource(filename);

        messageBodyPart2.setDataHandler(new DataHandler(source));
        messageBodyPart2.setFileName(filename);

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart2);
        multipart.addBodyPart(messageBodyPart1);
        message.setContent(multipart);
        Transport.send(message);

        System.out.println("=====Email Sent=====");

    } catch (MessagingException e) {

        throw new RuntimeException(e);

    }
}

Output Mail

In case of failure, an email is immediately sent to the concerned person with attached screenshot.

Important points

1.  Given properties will work only for Gmail. In case you are using Outlook or any other service as     per your project requirements, then these should be changed accordingly.

2.  Always call this code after test execution. You can use @AfterSuite of TestNG which run only when complete  suite execute.