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 “smtp.gmail.com”

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.


15 comments :

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

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

BUILD FAILED
D:\TestingEclipse\workspace\TestNG_Ant\build.xml:145: java.lang.ClassNotFoundExc
eption: javax.mail.internet.MimeMessage
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.tools.ant.taskdefs.email.EmailTask.execute(EmailTask.java:
452)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:811)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

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 ?

Thanks.

Varun Menon said...

Can you try with http://www.oracle.com/technetwork/java/javamail/index.html

and let me know.

Unknown said...

Hi Varun,

I downloaded "javamail-1.4.5" from this site(http://www.oracle.com/technetwork/java/javamail/index.html) .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.

Thanks.

madhusudhanareddy MV said...

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

Unexpected element "{}target" {antlib:org.apache.tools.ant}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,

send-report:
[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.

Regards,
Ankit

Software Testing said...

After adding jar files in eclipse. I am getting the below error.
Mail:
[mail] Sending email: test build
[mail] Failed to send email: Could not connect to SMTP host: smtp.gmail.com, 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