Javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 25; | Selenium Forum
C
Cheong Kah Seng Posted on 23/11/2020

Dear sir, 

I m facing smtp host error when I am trying to send the email from yahoo smtp. Do you have any idea on this issue? Thanks in advance. 

javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at com.qtpselenium.zoho.project.util.mail.SendMail.sendMail(SendMail.java:174)
at com.qtpselenium.zoho.project.util.mail.SendMail.main(SendMail.java:52)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:503)
at java.base/sun.nio.ch.Net.connect(Net.java:492)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
at java.base/java.net.Socket.connect(Socket.java:648)
at java.base/java.net.Socket.connect(Socket.java:597)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 5 more
javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at com.qtpselenium.zoho.project.util.mail.SendMail.sendMail(SendMail.java:174)
at com.qtpselenium.zoho.project.util.mail.SendMail.main(SendMail.java:71)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:503)
at java.base/sun.nio.ch.Net.connect(Net.java:492)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
at java.base/java.net.Socket.connect(Socket.java:648)
at java.base/java.net.Socket.connect(Socket.java:597)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 5 more


A
Ashish Thakur Replied on 24/11/2020

Plz check your yahoo mail settings


C
Cheong Kah Seng Replied on 25/11/2020

I have follow the following yahoo mail setting, can sir help check anything goes wrong? Note that i have modified sender and receiver emails and passwords to fake email and password which is not i the email and password that I am using during the test in my pc. Hope to hear from you soon. Thanks. 

 

package com.qtpselenium.zoho.project.util.mail;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

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

{
Properties prop=new Properties();
try {
FileInputStream fs = new FileInputStream(System.getProperty("user.dir")+"//src//test//resources//projectconfig.properties");
prop.load(fs);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//report folder - extent reports
//date
String reportFolder=prop.getProperty("reportFolder");
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;
String xsltReportPath=reportFolder+"Reports.zip";

// mail extent reports
String[] to={"receiver@gmail.com"};

String[] cc={};
String[] bcc={};

//This is for yahoo

sendMail("sender@yahoo.com",
"SenderPassword",
"smtp.mail.yahoo.com",
"25",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
"Automation Test Reports - Extent",
"Please find the reports attached.\n\n Regards\nWebMaster",
extentFilePath,
fileName);

// mail the xslt reports
Zip.zipDir(System.getProperty("user.dir")+"//XSLT_Reports", xsltReportPath);
SendMail.sendMail("sender@yahoo.com",
"SenderPassword",
"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;

}

}

 

}


A
Ashish Thakur Replied on 25/11/2020

yahoo SMTP server is not allowing connection

This means that you have to check you mail settings from inbox

You need to give permissions


Related Posts