Monday, July 25, 2011

Sending an E-mail with your Test Execution Reports

While doing automation testing many of us had thought that it is good to send the excetuion report mail after your test execution. In this blog I am going to tell two ways using which you can send a mail with to multiple E-mail addresses with your execution report attached.

Using ant:
Ant supports a mail task which can be used as one of your ways for sending a mail. Following target can be used to send a mail using ant:

In the above target you need to mention the following things:
mailhost – this the sender host of your mail box. You can this from your outgoing configuration of account in you mail client. For gmail it is “”

port – this is the port at which the above mailhost support sending of email. This configuration also you can check in your mailbox configuration. By default it is “25” but for SSL and TLS support it will be different for different hosts.

User/password – some mail hosts need the username and password of the account mail box to authenticate the user while sending a mail. You need to provide this in the “user” & “password” attributes.

ssl – if you want to use ssl for contacting the mailbox you need to set this attribute value to “true”

subject – Subject of the mail you want to give.

to – you need provide the address of the email recepients here using the “address” attribute. You use a property also here. This property you can setusing the properties file and then importing to the build.xml

attachments – this can be used to attach your report as part of the mail and then send it.

Please note: If you get a MIME error or java mail error while running the above target. Please download a jar of “java mail” and put it to the lib folder of your ant installation.

Using Java code:
When I had this problem of sending the execution report as mail, I did it in a hard way by writing the following java code. In the following code I had used the javax mail API for composing and sending the mail.


Anonymous said...

Thanks a Lot ,Varun :) :)

Siva said...

Hi Varun,

I followed the steps as you mentioned above. Under the lib folder i added (java-mail-1.4.4.jar) file as build path.

But when i execute the target (send), i got the below error in the cmd prompt.

D:\TestingEclipse\workspace\TestNG_Ant>ant send
Buildfile: D:\TestingEclipse\workspace\TestNG_Ant\build.xml

[mail] Failed to send email: javax.mail.internet.MimeMessage

D:\TestingEclipse\workspace\TestNG_Ant\build.xml:145: java.lang.ClassNotFoundExc
eption: javax.mail.internet.MimeMessage
at Method)
at java.lang.ClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
at java.lang.reflect.Method.invoke(

Total time: 0 seconds

Can you provide the solution for this.

Unknown said...
This comment has been removed by the author.
Unknown said...

Hi Varun,
I did same as you mention above. I am getting this error "Failed to send email: javax.mail.internet.MimeMessage" , "java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage"

Note: 1. I attached ant-javamail-1.6.3.jar file in libs folder
2. Need to create any separate class file for this or need to mention anything in class file?

Can you please guide me how to over come this issue ?


Varun Menon said...

Can you try with

and let me know.

Unknown said...

Hi Varun,

I downloaded "javamail-1.4.5" from this site( .I kept all jars file in libs folder.
After that i run, but i got same error as previously.

Please reply me , why i am getting this error?
"Failed to send email: javax.mail.internet.MimeMessage" , "java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage"

I sent you in mail my build.xml because here i am unable to attach xml file.

Please briefly explain.


madhusudhanareddy MV said...

Hi varun,,,,
I am getting below error...please guide me

Unexpected element "{}target" {}target

Anonymous said...
This comment has been removed by a blog administrator.
Unknown said...

Hi Varun,

I am trying to configure Send Email using ant but getting confused in what jars need to be included, could you please help me out in this?

I am getting following exception "java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage"

Even after adding javax.mail.jar, I am getting the same error.

Thanks in advance.

Natraj Photos said...

Download the javamail-1.4.7 and jaf-1.1.1 jar files add it in your ant lib path

In Eclipse
1. Navigate to Window -> Preferences -> Ant -> Runtime.
2. In Classpath tab select Global Entries
3. Click Add External JARs... and add the jar files. Click Apply and OK.
4. Goto Project -> Clean -> select Clean all Projects. Click OK.

Now execute your ant task.

Hope it helps

Unknown said...

After trying your suggestion, getting following error,

[mail] Failed to send email: javax.mail.internet.MimeMessage

Can you post an example send-report target of build.xml

Thanks in advance.

Unknown said...

I am using the one mentioned in Varun's original post.

Kumar Ankit said...

Hi Varun,

It sends the old copy of the report and is not refreshed.


Software Testing said...

After adding jar files in eclipse. I am getting the below error.
[mail] Sending email: test build
[mail] Failed to send email: Could not connect to SMTP host:, port: 465

Can you please help me..

Unknown said...

What if I have to generete email after finishing of my testng test cases.. How Can I include the code in Testng.xml file..

Post a Comment