How to send a report through email using Selenium Webdriver?

We can send a report through email using Selenium webdriver. The email report can be an important feature in an automation framework. An email should be necessarily sent after the combined execution of a regression suite has completed to get an overall view of the test results.

The ways to send a report through email are listed below −

Steps to configure the Java mail JAR are listed below −

Step1 − Navigate to the below link − and add the JAR to the project by clicking the highlighted link −

In case, we have a Maven project, add the dependency in the pom.xml file as highlighted in the below image −

Step2 − We should have the report generated in a separate file.


Code Implementation

import javax.mail.BodyPart;
import javax.mail.Session; import javax.mail.Transport; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.activation.DataSource; import javax.activation.FileDataSource;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailReport {
   public static void main(String[] args) {
      // instance of Properties class
      Properties p = new Properties();
      // configure host server
      p.put("", "");
      // configure socket port p.put("mail.smtp.socketFactory.port", "529");
      // configure socket factory
      p.put ("mail.smtp.socketFactory.class","");
      // configure true authentication p.put("mail.smtp.auth", "true");
      // configure smtp port
      p.put("mail.smtp.port", "465");
      // authentication with Session class
      Session s= Session.getDefaultInstance(p, new javax.mail.Authenticator() {
         protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("mail id", "password");
      try {
         // instance of MimeMessage
         Message m = new MimeMessage(s);
         // address of Form
         m.setFrom(new (""));
         //address of recipient m.setRecipients(Message.RecipientType.TO,InternetAddress.
         // email subject text
         m.setSubject("Email Report");
         // configure multi-media email
         BodyPart b = new MimeBodyPart();
         // email body text
         b.setText("Overall Selenium Test Report");
         // configure multi-media email for another text BodyPart b1 = new MimeBodyPart();
         // name of file to be attached to email
         String s = "C:\TestResults.xlsx";
         // pass filename
         DataSource sr= new FileDataSource(s);
         // set the handler
         b2.setDataHandler(new DataHandler(sr));
         // configure file
         // instance of class MimeMultipart
         Multipart mm = new MimeMultipart();
         // adding body texts
         // add email content
         // email sending
         System.out.println("MAIL TRIGGERED");
      } catch (MessagingException ex) {
         throw new RuntimeException(ex);