package com.qtpselenium.facebook.pom.mail;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import com.qtpselenium.facebook.pom.util.FBConstants;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;
public class SendMail
{
public static void main(String[] args) throws Exception
{
String reportFolder=FBConstants.REPORTS_PATH;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
FileFilterDateIntervalUtils filter =
new FileFilterDateIntervalUtils("2010-01-04", "2050-01-20");
File folder = new File(reportFolder);
File files[] = folder.listFiles(filter);
//date
String fileName=files[files.length-1].getName();
String extentFilePath=reportFolder+fileName;
// mail extent reports
String[] to={"raghunathar30@gmail.com","raghunathar30@outlook.com"};
String[] cc={};
String[] bcc={};
//This is for yahoo
sendMail("jrjsrs@yahoo.com",
"9980352938",
"smtp.mail.yahoo.com",
"25",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
"Automation Test Reports - Extent",
"Please find the attached reports .\n\n Regards\nWebMaster",
extentFilePath,
fileName);
// mail the xslt reports
String xsltReportPath=reportFolder+"Reports.zip";
Zip.zipDir(System.getProperty("user.dir")+"//XSLT_Reports", xsltReportPath);
SendMail.sendMail("jrjsrs@yahoo.com",
"9980352938",
"smtp.mail.yahoo.com",
"25",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
"Automation Test Reports - XSLT",
"Please find the reports attached.\n\n Regards\nWebMaster",
xsltReportPath,
"Reports.zip");
}
public static boolean sendMail(
final String userName,
final 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.starttls.enable", starttls);
props.put("mail.smtp.auth",auth);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
try
{
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, passWord);
}
});
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
//attachment start
// create the message part
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
// attachment ends
// Put parts in message
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;
}
catch (Exception mex)
{
mex.printStackTrace();
return false;
}
}
Exception i am getting :
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at com.qtpselenium.facebook.pom.mail.SendMail.sendMail(SendMail.java:171)
at com.qtpselenium.facebook.pom.mail.SendMail.main(SendMail.java:47)
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at com.qtpselenium.facebook.pom.mail.SendMail.sendMail(SendMail.java:171)
at com.qtpselenium.facebook.pom.mail.SendMail.main(SendMail.java:68)
Instructor
Ashish Thakur Replied on 19/03/2019
Please make sure that smtp is enabled in your yahoo account.
How to enable smtp in yahoo account?
Instructor
Ashish Thakur Replied on 20/03/2019
https://help.yahoo.com/kb/SLN4724.html;_ylt=AwrPgxBS0pFcF24AdcC7HAx.;_ylu=X3oDMTBycWJpM21vBGNvbG8Dc2czBHBvcwMxBHZ0aWQDBHNlYwNzcg--
Refer the url above
Where we will find these settings in yahoo web mail?
Instructor
Ashish Thakur Replied on 20/03/2019
just open the page and configure your account accordingly in the code