本文介绍了发送邮件:java.net.SocketException:网络无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试通过javax.mail发送邮件:
属性props = new Properties();
props.put(mail.smtp.host,xxxxx);
props.put(mail.smtp.port,25);
props.put(mail.smtp.auth,false);
会话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);
}
抛出异常
线程中的异常mainjava.lang.RuntimeException:javax.mail.MessagingException:无法连接到SMTP主机:srv-mail.imb.invention.com,port:25;
嵌套异常是:
java.net.SocketException:网络无法连接:连接
在foo.SendMailTest.main(SendMailTest.java:41)
导致:javax.mail .MessagingException:无法连接到SMTP主机:xxx.xxxx.zzzzz.com,port:25;
嵌套异常是:
java.net.SocketException:网络不可达:连接
在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
在com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
在javax.mail.Service.connect(Service.java:295)
在javax.mail.Service.connect (Service.java:176)
javax.mail.Service.connect(Service.java:125)
javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at foo.SendMailTest.main(SendMailTest.java:36)
导致:java.net.SocketException:网络无法访问:connect
在java.net.DualStackPlainSocketImpl.connect0(本机方法)
在java.net.DualStackPlainSocketImpl.socketConnect(未知来源)
在java.net.AbstractPlainSocketImpl.doConnect(未知来源)
在java.net.AbstractPlainSocketImpl.connectTo地址(未知来源)
在java.net.AbstractPlainSocketImpl.connect(未知源)
在java.net.PlainSocketImpl.connect(未知源)
在java.net.SocksSocketImpl.connect(未知源)
在java.net.Socket.connect(未知源)
在java.net.Socket.connect(未知源)
在com.sun.mail.util.SocketFetcher。 createSocket(SocketFetcher.java:321)
在com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport。 java:1927)
... 7更多
服务器可用:
C:\work\test> nc.exe xxx 25
220 xxx.zzz.aaaaaaa.com Microsoft ESMTP邮件服务准备在Thu,2013年9月12日15:10:45 +0300
ping也可以工作。
类似的.net代码按预期工作。
我没有想法出了什么问题....
答案是:java喜欢IPv6,但是在我们公司里有些错误。
添加属性
java.net.preferIPv4Stack = true
解决问题。 Try send mail via 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);
}
It throw exception
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
Server available:
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 also work.
Similar .net code work as expected.
I have no ideas what going wrong....
解决方案
The answer is: java prefer IPv6, but something wrong with it in our company.Adding property java.net.preferIPv4Stack=true
solves problem.
这篇关于发送邮件:java.net.SocketException:网络无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!