Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 无法连接到SMTP主机:localhost,端口:25;嵌套异常为:java.net.ConnectException:连接被拒绝:连接_Email_Jsp - Fatal编程技术网

Email 无法连接到SMTP主机:localhost,端口:25;嵌套异常为:java.net.ConnectException:连接被拒绝:连接

Email 无法连接到SMTP主机:localhost,端口:25;嵌套异常为:java.net.ConnectException:连接被拒绝:连接,email,jsp,Email,Jsp,我正在用jsp编写从本地主机发送电子邮件的应用程序&我发现类似的错误无法连接到SMTP主机:localhost,端口:25;嵌套异常为:java.net.ConnectException:连接被拒绝:connect 请检查并给我解决方案或想法,如果你有。为此,我使用下面的代码。 先谢谢你 <%@ page language="java" import="javax.naming.*,java.io.*,javax.mail.*, javax.mail.internet.*,com.sun

我正在用jsp编写从本地主机发送电子邮件的应用程序&我发现类似的错误无法连接到SMTP主机:localhost,端口:25;嵌套异常为:java.net.ConnectException:连接被拒绝:connect 请检查并给我解决方案或想法,如果你有。为此,我使用下面的代码。 先谢谢你

 <%@ page language="java" import="javax.naming.*,java.io.*,javax.mail.*,
javax.mail.internet.*,com.sun.mail.smtp.*"%>

<html>
<head>
<title>Mail</title>
</head>

<body>

<%
try{
  Session mailSession = Session.getInstance(System.getProperties());

  Transport transport = new SMTPTransport(mailSession,new URLName("localhost"));

  transport.connect("localhost",25,null,null);


  MimeMessage m = new MimeMessage(mailSession);

  m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%));

  Address[] toAddr = new InternetAddress[] {
              new InternetAddress(%><%request.getParameter("to")%><%)
            };
  m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );

  m.setSubject(%><%request.getParameter("subject")%><%);

  m.setSentDate(new java.util.Date());

  m.setContent(%><%request.getParameter("description")%><%, "text/plain");

  transport.sendMessage(m,m.getAllRecipients());

  transport.close();

  out.println("Thanks for sending mail!");
}
catch(Exception e){

  out.println(e.getMessage());
  e.printStackTrace();
}
%>


</body>

</html>

邮寄

首先,您必须确保有SMTP服务器正在侦听端口25

要查看您是否拥有该服务,可以尝试使用TELNET客户端,例如:

C:\> telnet localhost 25
(默认情况下,telnet客户端在最新版本的Windows上处于禁用状态,您必须从控制面板添加/启用Windows组件。在Linux/UNIX中,默认情况下通常存在telnet客户端

$ telnet localhost 25
如果它等待很长时间,然后超时,这意味着您没有所需的SMTP服务。如果成功连接,您可以输入一些内容并能够键入一些内容,该服务就在那里

如果您没有该服务,您可以使用以下服务:

  • 模拟SMTP服务器将模拟实际SMTP服务器的行为,当您使用Java时,建议使用模拟SMTP服务器是很自然的。这甚至可以在JUnit测试中工作(使用安装/拆卸/验证),或者作为单独的集成测试进程单独运行
  • 如果您的主机是Windows,那么在运行上述代码之前,您可以尝试在本地计算机上安装(还附带Apache Friends提供的WAMPP包)
  • 如果您的主机是Linux或UNIX,请尝试启用邮件服务,例如
  • Java中另一个完整的SMTP服务器,如邮件服务器
如果您确定您已经拥有该服务,则SMTP可能需要其他安全凭据。
如果您能告诉我在端口25上侦听的SMTP服务器,我可以告诉您更多信息。

CentOS 6和其他支持IPv6的服务器平台上的邮件服务器可能绑定到IPv6本地主机(::1),而不是IPv4本地主机(127.0.0.1)

典型症状:

[root@host /]# telnet 127.0.0.1 25
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

[root@host /]# telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 host ESMTP Exim 4.72 Wed, 14 Aug 2013 17:02:52 +0100

[root@host /]# netstat -plant | grep 25
tcp        0      0 :::25                       :::*                        LISTEN      1082/exim           
如果发生这种情况,请确保在
/etc/hosts
中没有两个具有不同IP地址的
localhost
条目,如以下(错误)示例:

为避免混淆,请确保只有一个
localhost
条目,最好是IPv4地址,如下所示:

[root@host /]# cat /etc/hosts
127.0.0.1 localhost  localhost.localdomain   localhost4.localdomain4 localhost4
::1       localhost6 localhost6.localdomain6

我安装了mercury电子邮件服务器,但仍然没有任何功能…telnet无法打开连接您可以运行“nslookup localhost”吗?我对它映射到哪个IP地址(IPv4或IPv6)感兴趣?您的解决方案是什么?请分享想法,让服务器端应用程序与之对话。现在,因为这是一个老问题的答案(当时是2011年),我正在更新此答案,因为似乎有很多人仍在想如何做到这一点。25可能不是端口。许多smtp服务器使用不同的端口。例如,eatj java hosting使用487。请确保您的传输实例已正确关闭,将其放在try finally块中以确保其正确。
[root@host /]# cat /etc/hosts
127.0.0.1 localhost  localhost.localdomain   localhost4.localdomain4 localhost4
::1       localhost6 localhost6.localdomain6