How to send a report through email using Selenium Webdriver?

SeleniumAutomation TestingTesting Tools

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 − https://mvnrepository.com/artifact/javax.mail/javax.mail-api/1.6.2 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.

Example

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("mail.smtp.host", "smtp.yahoo.com");
      // configure socket port p.put("mail.smtp.socketFactory.port", "529");
      // configure socket factory
      p.put ("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
      // 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 ("test12@yahoo.com"));
         //address of recipient m.setRecipients(Message.RecipientType.TO,InternetAddress.
         parse("test13@yahoo.com"));
         // 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
         b2.setFileName(s);
         // instance of class MimeMultipart
         Multipart mm = new MimeMultipart();
         // adding body texts
         mm.addBodyPart(b1);
         mm.addBodyPart(b);
         // add email content
         m.setContent(mm);
         // email sending
         Transport.send(m);
         System.out.println("MAIL TRIGGERED");
      } catch (MessagingException ex) {
         throw new RuntimeException(ex);
      }
   }
}
raja
Published on 29-Jun-2021 08:42:11
Advertisements