【问题标题】:Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect无法连接到 SMTP 主机:localhost,端口:25;嵌套异常是:java.net.ConnectException:连接被拒绝:连接
【发布时间】:2011-03-03 10:58:22
【问题描述】:

我正在申请在 jsp 中从本地主机发送电子邮件,我发现类似 无法连接到 SMTP 主机:本地主机,端口:25;嵌套异常是:java.net.ConnectException:连接被拒绝:连接 请检查并给我解决方案或想法,如果你有 .for 我正在使用下面的代码。 提前谢谢你。

 <%@ 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>

【问题讨论】:

  • 25 可能不是端口。许多 smtp 服务器使用不同的端口。例如eatj java托管使用487。
  • 确保您的传输实例已正确关闭,将其放入 try finally 块中以确保它。

标签: email jsp


【解决方案1】:

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

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

C:\> telnet localhost 25

(在最新版本的 Windows 上默认禁用 telnet 客户端,您必须从控制面板添加/启用 Windows 组件。在 Linux/UNIX 中,默认情况下通常有 telnet 客户端。

$ telnet localhost 25

如果等待时间过长然后超时,这意味着您没有所需的 SMTP 服务。如果连接成功,您可以输入内容并可以输入内容,则该服务就在那里。

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

  • 一个模拟 SMTP 服务器,它将模仿实际 SMTP 服务器的行为,因为您使用的是 Java,所以很自然地建议 Dumbster 假 SMTP 服务器。这甚至可以在 JUnit 测试中工作(使用设置/拆卸/验证),或者作为集成测试的单独进程独立运行。
  • 如果您的主机是 Windows,您可以在运行上述代码之前尝试在本地安装 Mercury email server(也附带 Apache Friends 的 WAMPP 包)。
  • 如果您的主机是Linux或UNIX,请尝试启用Postfix等邮件服务,
  • 另一个成熟的 Java SMTP 服务器,例如 Apache James 邮件服务器。

如果您确定您已经拥有该服务,则可能是 SMTP 需要额外的安全凭证。 如果你能告诉我监听 25 端口的 SMTP 服务器,我可以告诉你更多。

【讨论】:

  • 我安装了mercury电子邮件服务器,但仍然没有... telnet无法打开连接
  • 你能运行“nslookup localhost”吗?我对它映射到哪个 IP 地址(IPv4 或 IPv6)感兴趣?
  • 您的解决方案是什么?请分享
  • 这个想法是有一个服务器端应用程序可以与之交谈。现在,由于这是这个问题的旧答案(当时是 2011 年),我正在更新这个答案,因为似乎有很多人仍然想知道如何做到这一点。
【解决方案2】:

CentOS 6 和其他支持 IPv6 的服务器平台上的邮件服务器可能绑定到 IPv6 localhost (::1) 而不是 IPv4 localhost (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 条目,如下所示(错误)示例:

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

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

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

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 2012-12-21
    • 2013-06-24
    • 2016-08-13
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2014-03-04
    相关资源
    最近更新 更多