什么阻止了我的程序发送电子邮件?

什么阻止了我的程序发送电子邮件?

我有一个 Java 程序,它使用 javax.mail 发送 SMTP 消息。我想事先强调一下,这个程序在 Linux 机器上运行良好。当我在 Windows 7 x64 机器上尝试此代码时,我收到此错误:

send failed, exception: javax.mail.MessagingException: Could not connect to SMTP host:     smtp.west.cox.net, port: 25;
nested exception is:  java.net.SocketException: Network is unreachable: connect

以下是代码:

Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, props.getProperty("mail.to", "[email protected]"));
msg.setSubject(mySubject);
msg.setSentDate(new Date());
msg.setContent(sBuf.toString(), "text/html");
Transport.send(msg);

这个程序几乎对所有事情都使用默认设置。它在同一网络上的另一台机器上运行良好。它使用与我用于常规邮件客户端的相同设置,运行良好。这个 Windows 机器上有一些阻止 SMTP 的东西,但仅限于 Java。

我安装了 Symantec (Norton) 360。关闭它没什么区别,但重新启动进入安全模式(几乎禁用所有功能)可使程序正常运行并发送邮件。

总结一下:

  1. 程序代码有效。
  2. 设置正确。
  3. SMTP 适用于 Windows Mail,并且仅在此 Windows 计算机上被 Java 阻止。

在我花另一天时间拆开东西并卸载/重新安装之前,我想知道是否有人对解决这个问题有什么建议?

答案1

“无法连接到 SMTP 主机”和“网络不可达”表明根本原因是您的 Windows 计算机无法连接到 smtp.west.cox.net 计算机。这可能是由于以下原因之一造成的:

  • Windows 机器无法将 smtp.west.cox.net 解析为 IP(根据错误消息,不太可能)
  • Windows 计算机没有到该服务器 IP 的路由
  • 服务器 smtp.west.cox.net 不接受来自 Windows 服务器的连接(可能仅当您的 Windows 服务器与 Linux 服务器来自不同的 IP(从 SMTP 服务器的角度来看)时才会发生)

相关内容