Send Automation Reports as Mail Report


    Automation test reports are one of the important deliverable of Automation executions. Getting those automation reports in the mail automatically is the another important concept. Here, I would like to share the information about how to send your automation reports as mail report with attachment while automation of your application with Java or C# languages.

Send automation test reports with Java language:


        Need to add javax.mail dependency in pom.xml of your automation project (if it is a Maven project. Otherwise need to add javax.mail jar in your Java Build Path of your Java project). Following is the javax.mail dependency,


Following sendMailReport() method helps to send test automation reports with attachment,

public static void sendMailReport() throws AddressException, MessagingException {
String subject = “”, message = “”, maillist = “”, EMAIL_REPORT_FOLDER = “”;
subject = “Automation Test Report_”;
message = “Please find the attached report to get details.”;
maillist = “mail_lists_of_recipients”;
EMAIL_REPORT_FOLDER = “path_to_automation_report_folder”;
Properties properties = new Properties();
properties.put(“”, “”);
properties.put(“mail.smtp.port”, “587”);
properties.put(“mail.smtp.auth”, “true”);
properties.put(“mail.smtp.starttls.enable”, “true”);
final String userName;
final String password;
userName = “sender_mail_address”;
password = “sender_mail_account_password”;
properties.put(“mail.user”, userName);
properties.put(“mail.password”, password);

Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
Session session = Session.getInstance(properties, auth);
DateFormat dff = new SimpleDateFormat(“EEE MMM dd, yyyy HH:mm:ss z”);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(maillist));
msg.setSubject(subject + ” – ” + dff.format(new Date()).toString());
msg.setSentDate(new Date());

// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, “text/html”);

// creates multi-part
Multipart multipart = new MimeMultipart();

// adds attachments
List<String> filesList = new ArrayList<String>();
File[] files = new File(System.getProperty(“user.dir”) + EMAIL_REPORT_FOLDER).listFiles();
for (File file : files) {
if (file.isFile()) {
filesList.add(System.getProperty(“user.dir”) + EMAIL_REPORT_FOLDER + “/” + file.getName());
if (filesList != null && filesList.size() > 0) {
for (String filePath : filesList) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
} catch (IOException ex) {
// sets the multi-part as e-mail’s content
// sends the e-mail

Send automation test reports with C# language:

       The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery. Following Send_Report_In_Mail() method helps to send test automation reports with attachment,

public void Send_Report_In_Mail()
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(“”);
mail.From = new MailAddress(“sender_mail_address”);

StringBuilder TimeAndDate = new StringBuilder(DateTime.Now.ToString());
TimeAndDate.Replace(“/”, “_”);
TimeAndDate.Replace(“:”, “_”);

mail.Subject = “Automation Test Report_”+ TimeAndDate;

mail.Body = “Please find the attached report to get details.”;

string actualPath = AppDomain.CurrentDomain.BaseDirectory.Replace(“\\bin\\Debug”, “\\Test_Execution_Reports”); //Reports should stored in Test_Execution_Reports folder

var mostRecentlyModified = Directory.GetFiles(actualPath, “*.html”)
.Select(f => new FileInfo(f))
.OrderByDescending(fi => fi.LastWriteTime)

Attachment attachment;
attachment = new Attachment(mostRecentlyModified);

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(“sender_mail_address”, “sender_mail_account_password”);
SmtpServer.EnableSsl = true;



     You can use those method in @AfterSuite if you are using TestNG. In case of NUnit(C#), you can use those method in [OneTimeTearDown]. Try to use above code snippet in your automation scripts to send your automation test report as mail report automatically.

make it perfect !

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s