Pages

Wednesday, 14 December 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:

<target description="Generates and send junit test reports" name="send-report">
<mail mailhost="your mail host" mailport="your mail host port" password="your password" ssl="true" subject="Test build" user="your email id">
<from address="test@test.com"></from>
<replyto address="replyto@test.com"></replyto>
<to address="sento@test.com"></to>
<message>The Test nightly build has completed</message>
<attachments>
<fileset dir="attachment directory path">
<include name="**/*.zip"></include>
</fileset>
</attachments>
</mail>
</target>

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.

public void sendMessage(List<string> attachment)throws Exception{
Properties props = System.getProperties();
String host="SMTP_HOST";
String from="FROM_EMAIL";
String to="TO_EMAIL";
// Setup a mail server

//setting the host
props.put("mail.smtp.host", host);
Session session =
Session.getInstance(props, null);

// Define the mail message
MimeMessage message = new MimeMessage(session);
//setting the from address
message.setFrom(new InternetAddress(from));
//Creating an array of email addresses for sending the mail
javax.mail.internet.InternetAddress[] addressTo = javax.mail.internet.InternetAddress.parse(to);

for (int i = 0; i < addressTo.length; i++)
message.addRecipient(Message.RecipientType.TO, addressTo[i]);
//Setting the suject of the mail
message.setSubject(
"Automation TestSuite Report");

// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();

//fill message
messageBodyPart.setText("Hi,\n\nAttached is the Automation TestSuite Report for Test application.\n\nRegards,\nAutomation Team");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// The below secton is used to add attachments to the mail. A list String is sent with the apth of different files that needs to be attached.
messageBodyPart = new MimeBodyPart();
Iterator<string> itr = attachment.iterator();
while(itr.hasNext()){
messageBodyPart = new MimeBodyPart();
String filename=itr.next().toString();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
filename=filename.substring((filename.lastIndexOf("/"))+1);
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send( message );

}
</string></string>


No comments:

Post a Comment