发送邮件错误,javax.mail.MessagingException:无法连接到SMTP主机:本地主机,端口:25;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32882595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending mail error, javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
提问by Ketan G
Here is my code
这是我的代码
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSendClass {
public static void main (String [] args){
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("Thanks for registering on our website!");
// Now set the actual message
message.setText("Welcome To Job Portal !!!! Again Thanks ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
And I am getting this error everytime
我每次都会收到这个错误
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at MailSendClass.main(MailSendClass.java:58)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 7 more
BUILD SUCCESSFUL (total time: 3 seconds)
I am not getting the error why this is happening. Please help me in fixing this error.
我没有收到为什么会发生这种情况的错误。请帮助我修复此错误。
采纳答案by Serge Ballesta
Error is self explainatory: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
错误是不言自明的: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
You have no SMTPserver on localhost, but you configure it there :
您在本地主机上没有SMTP服务器,但您在那里配置它:
// Assuming you are sending email from localhost
String host = "localhost";
...
// Setup mail server
properties.setProperty("mail.smtp.host", host);
So you must:
所以你必须:
- either configure a local SMTP server as a relay on your local system (Postfix or sendmail are two well knows servers)
- of configure a dummy server that simply traces the mail request but does not even try to deliver mail (Python is known to have such dummy servers out of the box)
- or configure your application with a server that you are allowed to use - contact your system admin in a corporate environment, or your ISP in an individual one. Anyway, you will need that even to configure a true relay.
- 将本地 SMTP 服务器配置为本地系统上的中继(Postfix 或 sendmail 是两个众所周知的服务器)
- 配置一个虚拟服务器,它只是跟踪邮件请求,但甚至不尝试发送邮件(众所周知,Python 具有开箱即用的此类虚拟服务器)
- 或使用您可以使用的服务器配置您的应用程序 - 在公司环境中联系您的系统管理员,或在个人环境中联系您的 ISP。无论如何,您甚至需要它来配置真正的中继。
回答by emiliollbb
You should look at this two lines:
你应该看看这两行:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
Caused by: java.net.ConnectException: Connection refused: connect
The error is: "There is nothing listening on localhost at port 25".
错误是:“没有任何东西在 localhost 的 25 端口上侦听”。
You are trying to use localhost:25 as mail server, but there is no server there.
您正在尝试使用 localhost:25 作为邮件服务器,但那里没有服务器。
回答by QHuy
You should use the free Google SMTP server as a test.
您应该使用免费的 Google SMTP 服务器作为测试。
mail.host=smtp.gmail.com
mail.username=//your gmail
mail.password=//your password
mail.defaultEncoding=UTF-8
mail.smtp.auth=true
mail.smtp.starttls.required=true
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.smtp.socketFactory.port=465
Next, login with your gmail , and turn on less secure apps.
接下来,使用您的 gmail 登录,并打开安全性较低的应用程序。
回答by Zulfiqar Ali
Here is the working solution bro. it's guranteed
这是工作解决方案兄弟。这是有保证的
1) First of all open your gmail account from which you wanted to send mail, like in you case ""[email protected]"
1)首先打开您要从中发送邮件的gmail帐户,例如“”[email protected]”
2) open this link below https://support.google.com/accounts/answer/6010255?hl=en
2) 打开以下链接 https://support.google.com/accounts/answer/6010255?hl=en
3) click on "Go to the "Less secure apps" section in My Account." option
3) 单击“转到我的帐户中的“安全性较低的应用程序”部分。” 选项
4) Then turn on it
4)然后打开它
5) that's it (:
5)就是这样(:
回答by Harsh Sharma
Just use this provided solution : javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25
只需使用此提供的解决方案:javax.mail.MessagingException:无法连接到 SMTP 主机:本地主机,端口:25
and make sure to turn on less secure app access on your google account.
并确保在您的谷歌帐户上打开安全性较低的应用程序访问。