How to Email automated test Execution Reports as an attachment using Java Mail API in Selenium - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Monday 3 November 2014

How to Email automated test Execution Reports as an attachment using Java Mail API in Selenium

Hi Reader,


Sometimes there are situations when management people or your clients ask to send email after every test execution.


Here I am going to show you a sample Program in which we can automate the process of Sending Mail after every Test Execution in Selenium using Java Mail API.

Click here to Download the Java Mail jar file.


Glossary and utilities we will be using here are as follows:


SMTP: Acronym for Simple Mail Transfer Protocol. It provides a mechanism to deliver email.


We will be using 465 as SMTP Port to send the mail.


RecipientType can be To, CC,BCC like in any other mail.We are using 'To' here.


1.) We will be creating Session with Email id and password using following code.

Session session = Session.getDefaultInstance(props,newjavax.mail.Authenticator() 

{

protected PasswordAuthentication getPasswordAuthentication() 

{

return new PasswordAuthentication("GmailAddress","Password");

}

}

);

2.)We will be using Multipart.


It is used by browsers and HTTP clients to upload files to the server.

How data transfer take place..?Below is the sample http packet

POST http://127.0.0.1/GetPostRequest.php HTTP/1.1 

Host: 127.0.0.1 Connection: keep-alive 

Referer: http://localhost/GetPostRequest.php Content-Length: 1611568 Cache-Control: max-age=0 

Origin: http://localhost User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30 

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryX6nBO7q27yQ1JNbb 

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3


Note: In the above requestNotice the string "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryX6nBO7q27yQ1JNbb".

 
This is the part that tells the server: 


1) the HTTP request is a multipart request 

2) The separator between different chunks of data.

 
3.)Setting Subject and Message body using following code
message.setSubject("Script Status"); // used for setting Subject line
messageBodyPart.setText("PFA");


4.) Sending mail can be done using following snippet.

Transport.send(message);

Here is the whole code used for Sending Mail using Gmail account with Attachment of TestNG Reports

import java.util.Properties;

import javax.activation.DataHandler;


import javax.activation.DataSource;


import javax.activation.FileDataSource;


import javax.mail.Message;


import javax.mail.MessagingException;


import javax.mail.Multipart;


import javax.mail.PasswordAuthentication;


import javax.mail.Session;


import javax.mail.Transport;


import javax.mail.internet.InternetAddress;


import javax.mail.internet.MimeBodyPart;


import javax.mail.internet.MimeMessage;


import javax.mail.internet.MimeMultipart;

import org.apache.commons.lang3.exception.ExceptionUtils;



public class SendMail{


public static void ComposeGmail(String fromMail,String tomail)


{


Properties props = new Properties();

props.put("mail.smtp.host", "smtp.gmail.com");

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

props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

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

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

Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() 

{

protected PasswordAuthentication getPasswordAuthentication() 

{

return new PasswordAuthentication("GmailAddress","Password");}

}

);

try {

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(fromMail));

message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(tomail));

message.setSubject("Script Status");

MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText("PFA");


Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

messageBodyPart = new MimeBodyPart();

//Here for Sample I am attaching TestNG Report i.e emailable-report.html ,they exist in test-output folder

String file= System.getProperty("user.dir")+"\\test-output\\"+"emailable-report.html" ;

String fileName = "TestNG_Report.html";

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("Sent");

} catch (MessagingException ex)

{

throw new RuntimeException(ex);}

}

}


Note: We can use the function'ComposeGmail' anywhere between the different classes using following snippet


SendMail.ComposeGmail("fromEmailAddress","ToEmail Address");


No comments:

Post a Comment