Sending test report in mail
After executing our completed test suite now the new problem is how
to send Test report to your or client email id. Some time it takes 5-10
hours to complete a test execution. We are human being so we can not
just sit in front of system and wait till all the test execution will be
completed so in this case we add a new class in our program which send
email with attachment(Report file) to your email. Also some time this
is client requirement to send all the report files to him.
To send report in email, first you have to import activation and mail jar files to your program. You can download this from below links.
To run this class use below code :
To send report in email, first you have to import activation and mail jar files to your program. You can download this from below links.
Activation.jarNow add new java class in your project :
mail.jar
import javax.activation.DataHandler;Add this class as it is and use below code at the end of your test execution to call execute() method. Please change the value of email ids for "from" and "to" field to attach your email id in your code.
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
public static void execute() throws Exception
{
String filepath="your file path";
String[] to={"your email id "};
String[] cc={};
String[] bcc={};
SendMail.sendMail("username or email",
"password of above account",
"smtp.gmail.com",
"465",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",package library;
to,
cc,
bcc,
"Test Report",
"This is your gmail test report",
filepath,
"Test Report");
}
public static boolean sendMail(String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
boolean debug,
String socketFactoryClass,package library;
String fallback,
String[] to,package library;
String[] cc,
String[] bcc,
String subject,
String text,
String attachmentPath,
String attachmentName) throws Exception{
//Object Instantiation of a properties file.
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
package library;
if(!"".equals(port)){
props.put("mail.smtp.port", port);
}
if(!"".equals(starttls)){
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
}
if(debug){
props.put("mail.smtp.debug", "true");
}else{
props.put("mail.smtp.debug", "false");
}
if(!"".equals(port)){
props.put("mail.smtp.socketFactory.port", port);
}
if(!"".equals(socketFactoryClass)){
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
}
if(!"".equals(fallback)){
props.put("mail.smtp.socketFactory.fallback", fallback);
}
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
package library; msg.setSubject(subject);
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setFrom(new InternetAddress(userName));
for(int i=0;i<to.length;i++){
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(to[i]));
}
for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new
InternetAddress(cc[i]));
}
for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new
InternetAddress(bcc[i]));
}
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
}
To run this class use below code :
SendMail.execute();