Automatically Email the reports after Selenium test execution

How to automatically trigger email with reports after execution ?

There might be situations, where you might be tasked by the management people or your clients to send email after your every test execution. Here is a solution for that

This post exclusively written as requested by one of our fellow readers of this blog šŸ™‚

In this post we are going to look how we can send email to the clients
or stakeholders after the selenium test execution has been completed.

The program which we need include in Selenium Framework is,
Download here the Mail.jar
Download here the activation.jar

//The jar files which I have used are activation.jar and mail.jar Can be downloaded from Internet.

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

 public class SendMail

    //reportFileName = TestExecutionResultFileName
    public static void execute(String reportFileName) throws Exception

        String path=<Report file path>;

        String[] to={"<stakeholder1>","<stakeholder2>"};
        String[] cc={};
        String[] bcc={"<AutomationTester>"};

                            "<Subject line>",
                            "<Contents if any>",

      public  static boolean sendMail(String userName,
                String passWord,
                String host,
                String port,
                String starttls,
                String auth,
                boolean debug,
                String socketFactoryClass,
                String fallback,
                String[] to,
                String[] cc,
                String[] bcc,
                String subject,
                String text,
                String attachmentPath,
                String attachmentName){

        //Object Instantiation of a properties file.
        Properties props = new Properties();

        props.put("mail.smtp.user", userName);

        props.put("", host);

        props.put("mail.smtp.port", port);

            props.put("mail.smtp.auth", auth);


        props.put("mail.smtp.debug", "true");


        props.put("mail.smtp.debug", "false");


            props.put("mail.smtp.socketFactory.port", port);
            props.put("mail.smtp.socketFactory.fallback", fallback);


            Session session = Session.getDefaultInstance(props, null);


            MimeMessage msg = new MimeMessage(session);



            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(attachmentPath);
            messageBodyPart.setDataHandler(new DataHandler(source));

            msg.setFrom(new InternetAddress(userName));

            for(int i=0;i<to.length;i++){
                msg.addRecipient(Message.RecipientType.TO, new

            for(int i=0;i<cc.length;i++){
                msg.addRecipient(Message.RecipientType.CC, new

            for(int i=0;i<bcc.length;i++){
                msg.addRecipient(Message.RecipientType.BCC, new


            Transport transport = session.getTransport("smtp");

            transport.connect(host, userName, passWord);

            transport.sendMessage(msg, msg.getAllRecipients());


            return true;

        } catch (Exception mex){
            return false;

The above Source code will do the job of triggering email after every execution.

Where to how to use it?

Add the below snippet at the end of the test execution report creation.


With this the whole setup of automatic email triggering functionality is integrated with our framework.

25 Responses to “Automatically Email the reports after Selenium test execution”
  1. Kristal Asbury says:

    I didnt look for this, but I like this, found it enlightening! Keep up the great work!

  2. shashi says:

    NICE one and much required topic for me

  3. satish says:

    Please specify the path where can i get the above activation.jar and internet.jar files

  4. satish says:

    In the above code for this “String path=” What we need to give the “Report File path”
    And In this command “SendMail.execute(Execution File Name)” What we need to give the “Execution File Name”

    Please clarify above 2 things ,this would helpful to understand .


    • String path = (Just mention the path where your reports are generated at your local machine. eg: C:/seleniumworkspace/reports)

      SendMail.execute(Just specify the file name of the report created for the corresponding test execution)
      for eg. consider a report is generated for a test execution. This report will be located in C:/seleniumworkspace/reports/testcase1.html)
      In this case, the method we need to mention will be : SendMail.execute(testcase1.html)

  5. Umamaheshwar Thota says:

    Thanks assertselenium, it helped me a lot.

  6. Nishanth says:

    i am getting this error “javax.mail.AuthenticationFailedException”…and also it says as “535-5.7.1 Username and Password not accepted. Learn more at 535 5.7.1 d1sm66357567paz.17 – gsmtp”…
    i have mentioned my username and password correctly..but still it shows tis error….

  7. Navjot Kaur says:

    Hi Manoj,

    Nice Blog.I am trying to send report from gmail id to yahoo id by using above code. Subject and attachments are coming in yahoomail id. But message text is not coming.My testscript has run successfully.Please look at this issue.Thanks in advance..

  8. Navjot Handa says:

    Hi Manoj,

    Nice Blog.I am trying to send report from gmail id to yahoo id by using above code. Subject and attachments are coming in yahoomail id. But message text is not coming.My testscript has run successfully.Please look at this issue.Thanks in advance

  9. jehovabalan says:

    error throwed like system cannot find the path

    my file is in D:\email-report

    i gave path as “D:\”

    passed string is “email-report”

  10. ArvindRedy says:

    Hey it worked for me, Thankyou very much šŸ™‚

  11. Hariharan says:

    Superb, It works for me..

    Thanks Manoj

  12. Good Work Manoj………… Code really works!!!!!!!!!!!!!

    Keep up the good work… i really like it

    • Raghu says:

      Hi Can you post the working code please i am seeing lot many errors when i created a class with the above code , Do i need to import some jar files apart from mail.jar and activation.jar

  13. Naveen says:

    Hi assertselenium,

    The text and BCC doesn’t populate value in the email i.e. triggered on executing this code? Please assist.

  14. Lynn says:

    I got the following error javax.mail.MessagingException: Could not connect to SMTP host:, port: 465;
    nested exception is: Connection timed out: connect

    Any idea?

  15. shama says:


    I have used this code in my project, i did not get any error , but the mail is never sent. Could you plz tell me where must be the problem.

    Thanks and regards,
    Shama Ugale

  16. Manish says:

    Will it work for testng report ?

  17. Glad to see that my blog is posted on the official Selenium blog.

  18. Malvi says:

    Thanks ur code worked for me but only when i gave the path including the file name.

    String path=”D:\\….\test-output\\emailable-report.html”;

Check out what others are saying...
  1. […] is to use a CI container and have it email you, but if you are using Java and not using CI, then Automatically Email the Reports After Selenium Test Execution could be […]

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

%d bloggers like this: