0

I have to send an email of report of the test as soon as my all tests get executed. I will have to mention this in AfterSuite but don't know what exactly will have to use to achieve this. Any suggestion.

2

2 Answers 2

1

When you run your TCs through TestNG, a test-output folder is created, which contains a file with name emailable-report.html, so basically you need to send that file as an attachment to the stakeholders. Here I've considered the fact that your localhost is configured to send emails.

Please find below the code that you can use.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendFileEmail
{
   public static void main(String [] args)
   {

      // Recipient's email ID needs to be mentioned.
      String to = "[email protected]";
      // Sender's email ID needs to be mentioned
      String from = "[email protected]";
      // Assuming you are sending email from localhost
      String host = "localhost";
      // Get system properties
      Properties properties = System.getProperties();
      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);
         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));
         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Create the message part 
         BodyPart messageBodyPart = new MimeBodyPart();

         // Fill the message
         messageBodyPart.setText("This is message body");

         // Create a multipar message
         Multipart multipart = new MimeMultipart();

         // Set text message part
         multipart.addBodyPart(messageBodyPart);

         // Part two is attachment
         messageBodyPart = new MimeBodyPart();
         String filename = "<Enter File Path of Emailable Report>";
         DataSource source = new FileDataSource(filename);
         messageBodyPart.setDataHandler(new DataHandler(source));
         messageBodyPart.setFileName(filename);
         multipart.addBodyPart(messageBodyPart);

         // Send the complete message parts
         message.setContent(multipart );

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}
1
  • 1
    Could you explain the link contents? That way, the information is kept with the question, so if the link ever dies the information is still available here.
    – Kate Paulk
    Commented Jul 2, 2014 at 11:00
2

I did this by following code.

import java.util.Properties;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class emailReport {

public static void main(String[] args) {

    final String username = "give your email id";
    final String password = "enter your 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 javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("PFA");

        MimeBodyPart messageBodyPart = new MimeBodyPart();

        Multipart multipart = new MimeMultipart();

        messageBodyPart = new MimeBodyPart();
        String file = "path of file to be attached";
        String fileName = "attachment name";
        DataSource source = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(fileName);
        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        System.out.println("Sending");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        e.printStackTrace();
    }
  }
}

It worked fine for me.

3
  • 1
    With the above code when I am sending mails after a run of my suite then it mails the last emailable report instead of current one. Is there any way of sending mail after the new files are created. @Niels van Reijmersdal Commented Jul 15, 2014 at 13:03
  • 1
    Not sure why you asking me. I justed fixed the formating of your answer. But I think you should use a continuous intergration server. CI servers like Jenkins can send emails, collect reports, etc.. per build/test run. Commented Jul 15, 2014 at 14:20
  • @razizcool4all there's a Jenkins plugin for mailer, you would just add the mailer option to the end of the job.
    – corsiKa
    Commented Oct 11, 2014 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.