In this post I am going to explain how can we Email the reports after Selenium test execution
with the help of Java using java Mail libraries.
Download Javax Mail jar libraries in following link and configure to your project.
Download Javax Mail Jar
with the help of Java using java Mail libraries.
Download Javax Mail jar libraries in following link and configure to your project.
Download Javax Mail Jar
In this post
we are going to send Email through Gmail. So we have to configure SMTP
settings of it.Please find the below SMTP Settings of Gmail.
Please find
the sample code for the same.
Sample Code:
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
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;
public class SendMailUtility {
//Send mail
public boolean sendMail(String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
boolean debug,
String socketFactoryClass,
String fallback,
String[] to,
String[] cc,
String[] bcc,
String subject,
String text,
String attachmentPath,
String attachmentName){
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
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);
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
//attachment start
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
//Message body
messageBodyPart.addHeaderLine("Please find the attachement");
multipart.addBodyPart(messageBodyPart);
// Attachment ends
MimeMessage msg = new MimeMessage(session);
msg.setSubject(subject);
msg.setContent(multipart);
//msg.setText(text);
System.out.println("text================>"+text);
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;
}
catch (Exception mex)
{
mex.printStackTrace();
return false;
}
}
//usage
public static void main(String[] args){
SendMailUtility utility = new SendMailUtility();
//You can send to many number of members at a time by separating comma
String[] to={"testingteam@gmail.com","tl@gmail.com"};
String[] cc={"manager@gmail.com"};
String[] bcc={"testingteam@gmail.com@gmail.com"};
utility.sendMail(
"Sampletest@gmail.com",
"Test@123",
"smtp.gmail.com",
"465",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
"Automation Reports",
"Please find the attached reports",
"E:/WorkSpace/Practice/AutomationReports.zip",
"AutomationReports.zip");
System.out.println("Report has been sent through mail Successfully");
}
}
Here is a powerful article! Thank you for sharing such a wonderful and informative example. I found your article very useful and as an exchange of useful information I want to draw your attention to the cloud storage server. which can also be very useful in your work data room.
ReplyDeleteelazığ
ReplyDeletegümüşhane
kilis
siirt
sakarya
QHER0Z
bilecik
ReplyDeletevan
elazığ
tokat
uşak
EYİWİ6
D17BC
ReplyDeleteAydın Evden Eve Nakliyat
Afyon Evden Eve Nakliyat
Silivri Parke Ustası
Siirt Evden Eve Nakliyat
Çanakkale Evden Eve Nakliyat
DE75F
ReplyDeletekarabük en iyi ücretsiz görüntülü sohbet siteleri
tokat sesli sohbet siteler
hakkari görüntülü sohbet yabancı
en iyi rastgele görüntülü sohbet
niğde canlı sohbet bedava
erzincan telefonda sohbet
çankırı kızlarla rastgele sohbet
ankara tamamen ücretsiz sohbet siteleri
antep görüntülü sohbet yabancı
B965C
ReplyDeletetelefonda kadınlarla sohbet
elazığ sohbet odaları
nevşehir en iyi sesli sohbet uygulamaları
canlı sohbet ücretsiz
kars rastgele sohbet odaları
artvin kadınlarla rastgele sohbet
karaman seslı sohbet sıtelerı
mobil sohbet sitesi
sakarya mobil sohbet bedava
41FF5
ReplyDeletekocaeli sesli sohbet siteler
ücretsiz sohbet siteleri
Antep Görüntülü Sohbet Siteleri Ücretsiz
mobil sohbet siteleri
Muğla Canlı Sohbet Siteleri
mardin yabancı görüntülü sohbet siteleri
Nevşehir Parasız Görüntülü Sohbet
Nevşehir Ücretsiz Görüntülü Sohbet Uygulamaları
siirt ucretsiz sohbet
4445A
ReplyDeletesinop parasız sohbet
mardin rastgele görüntülü sohbet ücretsiz
Elazığ Telefonda Kızlarla Sohbet
şırnak canlı sohbet bedava
Adıyaman Bedava Sohbet Odaları
kırıkkale canlı sohbet siteleri
muş en iyi görüntülü sohbet uygulaması
Antalya Parasız Sohbet Siteleri
rastgele canlı sohbet
722F3
ReplyDeleteKwai Takipçi Satın Al
Bitcoin Madenciliği Siteleri
Fuckelon Coin Hangi Borsada
Nonolive Takipçi Hilesi
Okex Borsası Güvenilir mi
Bitcoin Oynama
Bitcoin Nasıl Üretilir
Sohbet
Osmo Coin Hangi Borsada
شركة عزل اسطح بالرس 8UkQexbOtx
ReplyDelete