Java Mail 之谜 - SMTP 被阻止?

2022-01-17 00:00:00 smtp java jakarta-mail

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

I have a Java program which uses javax.mail to send an SMTP message. This program works fine on a Linux box, I want to emphasize that beforehand. When I try this same code on my Windows 7 x64 box, I get this error:

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", "me@mine.com"));
msg.setSubject(mySubject);
msg.setSentDate(new Date());
msg.setContent(sBuf.toString(), "text/html");
Transport.send(msg);

这个程序几乎对所有东西都使用默认值.它在同一网络上的另一个盒子上工作正常.它使用的设置与我用于常规邮件客户端的设置相同,效果很好.这个 Windows 盒子上有一些东西阻止了 SMTP,但仅适用于 Java.

This program pretty much uses defaults for everything. It works fine on another box on the same network. It uses the same settings that I use for my regular mail client, which works fine. There is something on THIS Windows box that is blocking SMTP, but only for Java.

我安装了 Symantec (Norton) 360.关闭它没有任何区别,但重新启动到安全模式(几乎禁用所有东西)允许程序正常工作并发送邮件.

I have Symantec (Norton) 360 installed. Turning it off makes no difference, but rebooting into Safe Mode (which disables almost everything) allows the program to work and send mail just fine.

所以,回顾一下:

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

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

Before I spend another day tearing things apart and uninstalling / reinstalling, I wondered if anyone had any advice on fixing this?

推荐答案

问题是由于 Java 的 IPv4/IPv6 偏好.Java 尝试默认使用 IPv6 (?) 而我的 ISP 不支持 IPV6.但是,默认情况下,它在我的 Windows 7 机器上启用.

The problem is due to the IPv4/IPv6 preference of Java. Java tries to use IPv6 by default (?) and my ISP does not support IPV6. However, it's enabled on my Windows 7 boxes by default.

如果您遇到类似的行为,您可以通过转到控制面板/网络和 Internet/网络连接",右键单击您的 Internet 连接,然后从上下文菜单中选择状态"来验证这一点.状态对话框将显示两行类似的内容:

If you are experiencing similar behavior, you can verify this by going to "Control Panel/Network and Internet/Network Connections", right-clicking your internet connection, and selecting "Status" from the context menu. The Status dialog will display two lines similar to this:

IPv4 Connectivity:  Internet
IPv6 Connectivity:  No Internet access

这是问题的根源——Java 更喜欢 IPv6,它不能使用它来连接到 Internet.

This is the root of the problem - Java prefers IPv6, which it cannot use to connect to the internet.

要解决此问题,请执行以下其中一项(或两项)操作:

To fix this, do either one (or both) of these things:

  1. 在您的 Windows 7 机器上禁用 IPv6.
  2. 使用此命令行选项启动您的 java 程序:

  1. Disable IPv6 on your Windows 7 box.
  2. Start your java program using this command line option:

-Djava.net.preferIPv4Stack=true

执行其中任何一项都可以解决问题.

Doing either one of these will fix the problem.

相关文章