Introduction
In modern test automation, simply running tests is not enough — rapid communication of results is key. Emailing TestNG reports automatically ensures every stakeholder, from QA engineers to project managers, gets instant insights into test health. Whether you’re monitoring daily regression runs or validating a new release, this automation saves time, prevents oversight, and keeps your CI/CD pipeline efficient. By integrating report emailing into your Selenium TestNG framework, you not only accelerate feedback loops but also strengthen collaboration across teams.
What You’ll Learn
In this guide, you’ll learn how to:
– Configure TestNG to generate reports
– Write Java code to automatically email reports after test execution
– Use an SMTP server (Gmail or your organization’s mail server)
– Attach emailable-report.html to the mail
Prerequisites
Before you begin, ensure you have the following:
– A working Selenium + TestNG project
– Tests that generate test-output/emailable-report.html
– JavaMail API added as a dependency
If using Maven, add this to your pom.xml:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Step-by-Step: Emailing the Report
1. Create a Utility Class: EmailUtils.java
Here’s a sample EmailUtils.java code using JavaMail API:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;public class EmailUtils {
public static void sendReportByEmail() {
final String username = “your-email@gmail.com”;
final String password = “your-app-password”;Properties props = new Properties();
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.starttls.enable”, “true”);
props.put(“mail.smtp.host”, “smtp.gmail.com”);
props.put(“mail.smtp.port”, “587”);Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(“your-email@gmail.com”));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(“recipient@example.com”));
message.setSubject(“TestNG Report – ” + new Date());BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(“Please find the attached TestNG report.”);Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File(“test-output/emailable-report.html”));
multipart.addBodyPart(attachmentPart);message.setContent(multipart);
Transport.send(message);System.out.println(“Email Sent Successfully.”);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. Trigger It After Test Execution
Call the utility from an @AfterSuite method in your base test class:
@AfterSuite
public void sendReport() {
EmailUtils.sendReportByEmail();
}
Handling Gmail SMTP (If Using Gmail)
If you’re using Gmail:
1. Enable 2-Step Verification
2. Go to Google Account → Security → App Passwords
3. Generate an app-specific password (e.g., for “Mail”)
4. Use this app password in your code (not your regular Gmail password)
Folder Structure After Execution
After your test suite completes, TestNG generates:
– test-output/emailable-report.html
– test-output/index.html
You attach emailable-report.html in your email.
Optional: Rename the report with a timestamp for traceability.
Running from CLI or CI Tools
Use: mvn test
In Jenkins or other CI tools:
– Call EmailUtils.sendReportByEmail() from a post-build step or
– Include in the @AfterSuite of your base test class
Pro Tips
- Wait for the report file to be completely written
- Validate file path before attaching
- Compress large reports
- Attach logs/screenshots if needed
- Use CC/BCC wisely
Want to Go Next-Level?
Enhancements you can consider:
– Send HTML content in body instead of just attaching
– Embed screenshots or summaries
– Use ExtentReports instead of basic TestNG
– Convert report to PDF and send
– Integrate with Slack, Teams, etc. via webhook
Conclusion
Automating TestNG report delivery transforms how teams respond to quality issues. It reduces manual effort, accelerates feedback loops, and ensures complete transparency in testing. In Selenium automation testing, this approach not only streamlines result sharing but also enhances collaboration across teams. Once you’ve mastered email automation, you can extend the process to include rich HTML dashboards, PDF reports, or even AI-driven analytics for proactive defect detection. In short, this is not just a time-saver — it’s a quality culture enabler.
FAQs
1. What is TestNG report automation?
TestNG report automation is the process of automatically generating and sending TestNG test execution results to stakeholders. This eliminates manual report sharing, speeds up communication, and ensures teams have instant visibility into test outcomes.
2. How does automated TestNG report delivery help in Selenium automation testing?
In Selenium automation testing, automated TestNG report delivery ensures that test results are instantly available to developers, testers, and managers. This accelerates defect identification, improves collaboration, and supports continuous integration and delivery (CI/CD) pipelines.
3. What are the benefits of emailing TestNG reports automatically?
-
Reduces manual effort in sharing results
-
Speeds up feedback loops for faster bug fixes
-
Increases transparency across teams
-
Supports better decision-making with real-time data
4. Can I customize TestNG reports before sending them?
Yes. You can enhance TestNG reports with HTML formatting, PDF exports, or even integrate AI-driven analytics to highlight critical issues and trends before sending them via email.
5. How do I integrate TestNG report emailing into my test framework?
You can use JavaMail API or similar email libraries in your test framework to send TestNG reports after execution. This can be part of your post-test actions in Selenium or integrated with CI/CD tools like Jenkins for seamless delivery.
6. Is it possible to send TestNG reports with visual dashboards?
Absolutely. Many teams extend their automation to include rich HTML dashboards or data visualizations along with standard reports, making it easier for stakeholders to interpret results quickly.
We Also Provide Training In:
- Advanced Selenium Training
- Playwright Training
- Gen AI Training
- AWS Training
- REST API Training
- Full Stack Training
- Appium Training
- DevOps Training
- JMeter Performance Training
Author’s Bio:
As a Senior SDET with 8+ years in testing and development, I build scalable automation platforms ensuring quality at speed. Passionate about mentoring and innovation, I equip teams with real-time solutions and high-impact frameworks, driving excellence through continuous learning. Let’s shape the future of quality engineering together.
Dilipkumar Rajendran
Senior SDET | Playwright & Selenium Expert