How to Set Up Email Notifications After Test Execution with EmailAttachmentsSender in automationframeworkselenium
The automationframeworkselenium project automatically dispatches Extent HTML reports via email after test suite completion by enabling flags in config.properties, configuring SMTP credentials in EmailConfig.java, and allowing the TestListener to invoke EmailSendUtils, which delegates to EmailAttachmentsSender for JavaMail delivery.
The anhtester/automationframeworkselenium repository provides a built-in mechanism to notify stakeholders immediately after test execution finishes. This guide explains how to configure the framework to send multipart emails with the Extent report attached using the EmailAttachmentsSender utility class.
Enable Email Notifications in config.properties
The framework checks configuration flags before attempting to send mail. Open src/test/resources/config/config.properties and set the following values:
# Enable automatic email dispatch
SEND_EMAIL_TO_USERS = yes
# Ensure consistent report naming for attachment
OVERRIDE_REPORTS = yes
# Optional: compress the report folder before sending
ZIP_FOLDER = yes
ZIP_FOLDER_PATH = exports/ExtentReports
ZIP_FOLDER_NAME = ExtentReports.zip
When SEND_EMAIL_TO_USERS equals yes, the EmailSendUtils class proceeds to build and transmit the message after the suite finishes.
Configure SMTP Settings in EmailConfig.java
The EmailConfig.java file stores static constants for server authentication and recipient lists. Edit src/main/java/com/anhtester/mail/EmailConfig.java with your provider details:
package com.anhtester.mail;
public class EmailConfig {
// Gmail SMTP settings (modify for your provider)
public static final String SERVER = "smtp.gmail.com";
public static final String PORT = "587";
// Sender authentication - use app-specific passwords
public static final String FROM = "your.email@gmail.com";
public static final String PASSWORD = "your-app-password";
// Recipient array supports multiple addresses
public static final String[] TO = {
"team.lead@example.com",
"qa.manager@example.com"
};
public static final String SUBJECT = "Automation Test Execution Report";
}
Security note: For Gmail accounts, you must generate an App Password or enable appropriate security settings rather than using your primary account password.
How the Email Delivery Pipeline Works
The framework implements a three-stage pipeline that triggers automatically when the TestNG suite completes.
TestListener Invokes the Sender on Suite Finish
The TestListener class implements ISuiteListener and overrides onFinish. Located at src/test/java/com/anhtester/listeners/TestListener.java, it collects test counts and calls the utility method:
@Override
public void onFinish(ISuite iSuite) {
// Flush Extent reports and generate files
ExtentReportManager.flushReports();
// Calculate statistics
int total = count_totalTCs;
int passed = count_passedTCs;
int failed = count_failedTCs;
int skipped = count_skippedTCs;
// Trigger email dispatch
EmailSendUtils.sendEmail(total, passed, failed, skipped);
}
EmailSendUtils Prepares Content and Report Path
The EmailSendUtils class (located at src/main/java/com/anhtester/utils/EmailSendUtils.java) constructs an HTML summary table and locates the report file:
public static void sendEmail(int total, int passed, int failed, int skipped) {
if (FrameworkConstants.SEND_EMAIL_TO_USERS.trim()
.equalsIgnoreCase(FrameworkConstants.YES)) {
String body = getTestCasesCountInFormat(total, passed, failed, skipped);
String reportPath = FrameworkConstants.getExtentReportFilePath();
try {
EmailAttachmentsSender.sendEmailWithAttachments(
EmailConfig.SERVER,
EmailConfig.PORT,
EmailConfig.FROM,
EmailConfig.PASSWORD,
EmailConfig.TO,
EmailConfig.SUBJECT,
body,
reportPath
);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
The getTestCasesCountInFormat method generates an HTML table displaying total, passed, failed, and skipped test counts.
EmailAttachmentsSender Handles JavaMail Multipart Logic
The EmailAttachmentsSender class at src/main/java/com/anhtester/mail/EmailAttachmentsSender.java performs the low-level JavaMail operations:
public static void sendEmailWithAttachments(
String host, String port, String user, String pass,
String[] toAddress, String subject, String message,
String... attachFiles) throws MessagingException {
// Create authenticated session
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
});
// Build multipart message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user));
for (String to : toAddress) {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
msg.setSubject(subject);
// Add HTML body part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attach Extent report file
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(filePath);
multipart.addBodyPart(attachPart);
}
msg.setContent(multipart);
Transport.send(msg);
}
The method creates a MimeMultipart message, attaches the Extent HTML report located at exports/ExtentReports/ExtentReports.html, and transmits via Transport.send().
Maven Dependencies
The required JavaMail API is already declared in the project pom.xml. Verify the dependency exists:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
No additional libraries are required to use EmailAttachmentsSender.
Summary
- Enable notifications by setting
SEND_EMAIL_TO_USERS = yesinsrc/test/resources/config/config.properties. - Configure credentials in
src/main/java/com/anhtester/mail/EmailConfig.javawith your SMTP host, port, sender address, and recipient list. - Trigger mechanism is handled automatically by
TestListener.onFinish(), which invokesEmailSendUtils.sendEmail(). - Attachment handling occurs in
EmailAttachmentsSender.sendEmailWithAttachments(), which uses JavaMail to create a multipart message with the Extent report. - Execution requires only running
mvn clean test; the email dispatches immediately after the suite completes.
Frequently Asked Questions
Does automationframeworkselenium support multiple email recipients?
Yes. The EmailConfig.java file defines TO as a String[] array, allowing you to specify multiple addresses. The EmailAttachmentsSender iterates over this array and adds each recipient to the MimeMessage using Message.RecipientType.TO.
How do I secure SMTP credentials in EmailConfig.java?
Store sensitive credentials in environment variables or external property files excluded from version control, then modify EmailConfig.java to read these values at runtime. Never commit actual passwords to the repository; use GitHub Secrets or a local config.properties override file instead.
Can I attach additional files beyond the Extent report?
Yes. The sendEmailWithAttachments method accepts a varargs parameter String... attachFiles. You can pass multiple file paths from EmailSendUtils by extending the argument list to include screenshots, log files, or zipped archives generated during test execution.
What triggers the email if I run tests outside of Maven?
The email triggers whenever the TestNG suite listener completes, regardless of the runner. Whether you execute tests via IDE (IntelliJ IDEA, Eclipse), command-line TestNG, or CI/CD pipelines, the TestListener.onFinish() method executes and initiates the email dispatch provided SEND_EMAIL_TO_USERS remains enabled.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →