gpt4 book ai didi

java - 发送邮件 : java.net.SocketException:网络无法访问:连接

转载 作者:搜寻专家 更新时间:2023-10-30 21:14:40 26 4
gpt4 key购买 nike

尝试通过 javax.mail 发送邮件:

Properties props = new Properties();
props.put("mail.smtp.host", "xxxxx");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "false");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xx", "xx");
}
});

try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xxxxx"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xxxxx"));
message.setSubject("Subject");
message.setText("Body");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

抛出异常

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: srv-mail.imb.invention.com, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect
at foo.SendMailTest.main(SendMailTest.java:41)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: xxx.xxxx.zzzzz.com, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at foo.SendMailTest.main(SendMailTest.java:36)
Caused by: java.net.SocketException: Network is unreachable: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 7 more

服务器可用:

C:\work\test>nc.exe xxx 25
220 xxx.zzz.aaaaaaa.com Microsoft ESMTP MAIL Service ready at Thu, 12 Sep 2013 15:10:45 +0300

ping 也可以。

类似的 .net 代码按预期工作。

我不知道出了什么问题......

最佳答案

答案是:java比较喜欢IPv6,但是我们公司有问题。添加属性 java.net.preferIPv4Stack=true 解决问题。

关于java - 发送邮件 : java.net.SocketException:网络无法访问:连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com