import jakarta.mail.;
import jakarta.mail.internet.;
import java.io.File;
import java.util.Properties;
public class ExecutionEmailService {
private static final String SMTP_HOST = "smtp.yourserver.com";
private static final String SMTP_PORT = "587";
private static final String USERNAME = "[email protected]";
private static final String PASSWORD = "your_password";
private static final String RECIPIENT = "[email protected]";
private static Session createSession() {
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
return Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
}
private static void sendEmail(String subject, String body, String htmlFilePath) {
try {
MimeMessage message = new MimeMessage(createSession());
message.setFrom(new InternetAddress(USERNAME));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(RECIPIENT));
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(body);
multipart.addBodyPart(textPart);
if (htmlFilePath != null && new File(htmlFilePath).exists()) {
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.attachFile(htmlFilePath);
multipart.addBodyPart(htmlPart);
}
message.setContent(multipart);
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
// 📤 1. Execution Start Email
public static void sendEmailDuringExecutionStart() {
sendEmail("Test Execution Started", "Execution has started successfully.", null);
}
// ✅ 2. All Test Cases Passed
public static void sendEmailDuringPassScenario(String passedHtmlPath) {
sendEmail("Test Execution Passed", "All test cases passed successfully.", passedHtmlPath);
}
// ❌ 3. Some Test Cases Failed
public static void sendEmailDuringFailScenario(String failedHtmlPath) {
sendEmail("Test Execution Failed", "Some test cases failed. Please review the report.", failedHtmlPath);
}
}
public class ExecutionMailListener implements ITestListener {
private boolean hasFailure = false;
@Override
public void onStart(ITestContext context) {
ExecutionEmailService.sendEmailDuringExecutionStart();
}
@Override
public void onTestFailure(ITestResult result) {
hasFailure = true;
}
@Override
public void onFinish(ITestContext context) {
if (hasFailure) {
ExecutionEmailService.sendEmailDuringFailScenario("path/to/failedTestcases.html");
} else {
ExecutionEmailService.sendEmailDuringPassScenario("path/to/passedTestcases.html");
}
}
}
https://www.automatetheplanet.com/playwright-tutorial-iframe-and-shadow-dom-automation/