Java 邮件 - 无法连接到主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29974282/
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
Java mail - Couldn't connect to host
提问by Sarpy
I am trying to send an email Java application, following guide on http://www.tutorialspoint.com/java/java_sending_email.htm
我正在尝试按照http://www.tutorialspoint.com/java/java_sending_email.htm 上的指南发送电子邮件 Java 应用程序
When I try to run it, copypasted first code from link above, I get an error that it could not connect to host, which is a localhost. Here is a result when I ping localhost:
当我尝试运行它时,从上面的链接复制粘贴第一个代码,我收到一个错误,提示它无法连接到主机,这是一个本地主机。这是我 ping localhost 时的结果:
Pinging Home-PC [::1] with 32 bytes of data:
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Since I am greatly inexperienced in communications, I do not know what went wrong, and yet again, I need help. So, is the issue in my localhost not being properly configured, or is something wrong with my code? What could I do?
由于我在沟通方面非常缺乏经验,我不知道出了什么问题,再次需要帮助。那么,是我的 localhost 中的问题没有正确配置,还是我的代码有问题?我能做什么?
I am using Tomcat v8.0 server.
我正在使用 Tomcat v8.0 服务器。
I am also adding console output:
我还添加了控制台输出:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at test.Test.main(Test.java:51)
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:329)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2020)
... 7 more
回答by Thomas Junk
I assume, that you do not have an SMTP
-Server installed on your local machine. For testing purposes, start an instace of FakeSMTP. If it is up and running, programs are able to connect and to drop their mail.
我假设您没有SMTP
在本地计算机上安装 -Server。出于测试目的,启动 FakeSMTP 的一个实例。如果它启动并运行,程序就能够连接并丢弃它们的邮件。
If you want to experiment a little (assuming you have a Windows box), there is Hamstermail(Site is in german - be warned ;-))
如果你想尝试一下(假设你有一个 Windows 盒子),有Hamstermail(站点是德语 - 请注意;-))
Here is a result when I ping localhost
这是我 ping localhost 时的结果
That only shows that a server responds to an Echo Request
. This does neither mean, that the server has a SMTP-service running, nor does it mean, if the server does not answer, that the server is down - sometimes, admins block ICMP-Echo-Requests
.
这仅表明服务器响应Echo Request
. 这既不意味着服务器正在运行 SMTP 服务,也不意味着如果服务器没有响应,则服务器已关闭 - 有时,管理员会阻止ICMP-Echo-Requests
.
Sometimes a telnet $server 25
allows connecting to the mail-server.
有时telnet $server 25
允许连接到邮件服务器。
Caused by: java.net.ConnectException: Connection refused: connect
引起:java.net.ConnectException:连接被拒绝:连接
This shows, that the Service is not active aka the port is closed.
这表明该服务不活动,即端口已关闭。