Java 邮件之谜 - SMTP 被阻止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14064111/
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 mystery - SMTP blocked?
提问by user1071914
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:
我有一个使用 javax.mail 发送 SMTP 消息的 Java 程序。这个程序在 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
Here is the code:
这是代码:
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);
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.
该程序几乎对所有内容都使用默认值。它在同一网络上的另一个盒子上工作正常。它使用的设置与我用于常规邮件客户端的设置相同,效果很好。在这个 Windows 机器上有一些东西会阻止 SMTP,但仅适用于 Java。
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.
我安装了赛门铁克(诺顿)360。关闭它没有任何区别,但重新启动到安全模式(几乎禁用所有功能)可以让程序正常工作并发送邮件。
So, to recap:
所以,回顾一下:
- The program code works.
- The settings are correct.
- SMTP works for Windows Mail and is only blocked for Java on this Windows machine.
- 程序代码有效。
- 设置正确。
- 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?
在我又花一天时间拆解并卸载/重新安装之前,我想知道是否有人对解决此问题有任何建议?
回答by user1071914
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.
问题是由于 Java 的 IPv4/IPv6 偏好造成的。Java 尝试默认使用 IPv6 (?) 而我的 ISP 不支持 IPV6。但是,它默认在我的 Windows 7 机器上启用。
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:
如果您遇到类似的行为,您可以通过转到“控制面板/网络和 Internet/网络连接”,右键单击您的 Internet 连接,然后从上下文菜单中选择“状态”来验证这一点。“状态”对话框将显示与此类似的两行:
IPv4 Connectivity: Internet
IPv6 Connectivity: No Internet access
This is the root of the problem - Java prefers IPv6, which it cannot use to connect to the internet.
这是问题的根源 - Java 更喜欢 IPv6,它不能用于连接到 Internet。
To fix this, do either one (or both) of these things:
要解决此问题,请执行以下一项(或两项)操作:
- Disable IPv6 on your Windows 7 box.
Start your java program using this command line option:
-Djava.net.preferIPv4Stack=true
- 在 Windows 7 机器上禁用 IPv6。
使用以下命令行选项启动您的 Java 程序:
-Djava.net.preferIPv4Stack=true
Doing either one of these will fix the problem.
执行其中任何一项都可以解决问题。
回答by mariusz117
I had the same problem during upgrade form java 1.6 to java 1.7. The problem occured because java 1.7 using IPv6 by default. To fix this, you need to add Java Option like on example below.
我在从 java 1.6 升级到 java 1.7 的过程中遇到了同样的问题。出现问题是因为 java 1.7 默认使用 IPv6。要解决此问题,您需要像下面的示例一样添加 Java 选项。
Just run this command on Windows cmd:
只需在 Windows cmd 上运行此命令:
setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true
回答by sengokyu
Synantec EP 12.1 RU5fixes your issue.
Synantec EP 12.1 RU5解决了您的问题。
Symantec Endpoint Protection blocks email sent by Java mail client
Fix ID: 3399185
Symptom: The Internet Email Auto-Protect feature of Symantec Endpoint Protection blocks SMTP email sent by a Java mail client.
Solution: Updated the Internet Email Auto-Protect driver to allow processing of IPv4-mapped IPv6 addresses.
Symantec Endpoint Protection 阻止 Java 邮件客户端发送的电子邮件
修复 ID:3399185
症状:Symantec Endpoint Protection 的 Internet 电子邮件自动防护功能会阻止 Java 邮件客户端发送的 SMTP 电子邮件。
解决方案:更新了 Internet 电子邮件自动防护驱动程序以允许处理 IPv4 映射的 IPv6 地址。
回答by sbpandey
I was also facing the same issue , basically it was a Java 7 issue, Just passing in JVM argument "-Djava.net.preferIPv4Stack=true", i got rid from issue .
我也面临同样的问题,基本上是 Java 7 问题,只需传入 JVM 参数“-Djava.net.preferIPv4Stack=true”,我就摆脱了问题。
回答by monojohnny
Although this issue is solved: it might still be useful to point out that some Windows Firewalls block/allow access to ports based on the executable name (rather than purely the port number itself) - so you can end up with one program (say Outlook) being allowed and another (say Java) being blocked.
尽管此问题已解决:指出某些 Windows 防火墙根据可执行文件名称(而不是纯粹的端口号本身)阻止/允许访问端口可能仍然有用-因此您最终可以得到一个程序(例如 Outlook ) 被允许,另一个(比如 Java)被阻止。
回答by Renu
I had similar issue but strangely everything was working fine with jdk 1.6 once I install jdk 1.7 I got the exception. I wonder what could be the reason for this!
我有类似的问题,但奇怪的是,当我安装 jdk 1.7 后,jdk 1.6 一切正常,但出现异常。我想知道这可能是什么原因!
After adding IPV4 argument it is working fine..Thank you so much.
添加 IPV4 参数后,它工作正常..非常感谢。