How to send a report through email using Selenium Webdriver?
Last Updated :
23 Jul, 2025
In the world of automation testing, generating and sharing test reports is crucial. After executing tests using Selenium WebDriver, it's often necessary to share the results with team members or stakeholders. Automating the process of sending these reports via email can save time and ensure that everyone stays informed about the testing outcomes.
In this article, we'll explore how to send a report through email using Selenium WebDriver, breaking down the steps and providing a complete program for easy implementation.
Sending Report Through Email in Selenium WebDriver
1. Set Up Your Environment
Before you start, make sure you have the necessary dependencies in your project. You'll need Selenium WebDriver for your test automation and JavaMail API to handle email sending. You can include these in your Maven project using the following dependencies:
XML
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.8.0</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
2. Create the Test Report
Execute your Selenium tests and generate the test report. You can use tools like TestNG or JUnit to create reports. These reports are typically in HTML or XML format. Ensure the report is saved in a known location as you'll need to attach it to your email.
3. Set Up JavaMail API
The JavaMail API allows you to send emails programmatically. You'll need to configure the SMTP server settings (like Gmail, Yahoo, etc.) to send emails. Here’s a simple setup using Gmail:
Java
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
4. Authenticate the Email Account
You'll need to authenticate the email account you're using to send the report. This is done using the Session object in JavaMail API:
Note: This is not the password by which you login to gmail, instead you need to generate an app password from your Google Account's Security settings. Link.
Make sure you used the right path of report file.
Java
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "your_password");
}
});
5. Compose the Email
Now, you can compose the email by setting the subject, recipients, and body. You can also attach your test report to the email:
Java
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Automated Test Report");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Please find the attached test report.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attach the report
messageBodyPart = new MimeBodyPart();
String filename = "path_to_report/report.html";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
6. Run the Program
After setting up the email-sending code, integrate it with your test automation framework. You can call this email-sending function at the end of your test execution to automatically send the report.
Complete Program to Send Reports Through Email in Selenium WebDriver
Here's a complete example program that ties everything together:
Java
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmailWithReport {
public static void main(String[] args) {
// Set up the SMTP server settings
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// Authenticate the email account
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "your_password");
}
});
try {
// Compose the email
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Automated Test Report");
// Email body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Please find the attached test report.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attach the report
messageBodyPart = new MimeBodyPart();
String filename = "path_to_report/report.html";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Combine message parts
message.setContent(multipart);
// Send the email
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Output:
Email sent successfully.
Conclusion
Sending test reports through email is an essential part of the automation testing process. By following the steps outlined in this article, you can seamlessly integrate email reporting into your Selenium WebDriver projects. This automated process not only saves time but also ensures that your team stays up-to-date with the latest test results.
Whether you're using Gmail or another email service, the JavaMail API offers a flexible and reliable way to send reports programmatically.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING