傳送自動郵件時Could not connect to SMTP host: 10.1.1.195, port: 25;報錯的解決方法
阿新 • • 發佈:2019-02-02
最近在測試公司內部系統自動傳送郵件功能時,一直在報錯:
javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: Could not connect to SMTP host: 10.1.1.195, port: 25; nested exception is: java.net.SocketException: Network is unreachable: connect at javax.mail.Transport.send0(Transport.java:219) at javax.mail.Transport.send(Transport.java:81) at com.gree.util.MailUtil.sendMessage(MailUtil.java:241) at com.gree.util.MailUtil.sendTest(MailUtil.java:1112) at com.gree.util.MailUtil.main(MailUtil.java:1120)</span>
在同事的電腦上同樣的專案同樣的程式碼都沒問題,所以排除了伺服器錯誤的問題,通過telnet測試埠也沒問題,也可以ping通,最後發現原來是tomcat伺服器缺少某些啟動引數。。解決方法:在tomcat配置中加入該引數
Servers->雙擊Tomcat->Open launch configuration->Arguments->在VM arguments輸入框最後新增下面這句:
-Djava.net.preferIPv4Stack=true</span>
然後在main函式開頭新增下面這句:
再測試一下,果然不再報錯,郵件順利傳送出去了~System.setProperty("java.net.preferIPv4Stack", "true");</span>