2017-08-23 141 views
0

而發送的電子郵件顯示錯誤:無法連接到SMTP主機:smtp8.net4india.com,端口:25;

javax.mail.MessagingException: Could not connect to SMTP host: smtp8.net4india.com, port: 25; 
    nested exception is: 
    java.net.ConnectException: Connection timed out: connect 

我的電子郵件地址設置爲:

 String mailProcessed = ""; 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp8.net4india.com"); 
     props.put("mail.smtp.socketFactory.port", "25"); 
     props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "25"); 
     props.put("mail.smtp.starttls.enable", "true"); 

     Session session = Session.getDefaultInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(frmMail, frmPass); 
        } 
       }); 

回答

0

刪除props.put("mail.smtp.starttls.enable", "true");

,並添加props.put("mail.smtp.ssl.enable", "true");

您使用SSL,TLS沒有。

還要確保郵件服務器支持SSL。

還要確保郵件服務器使用TCP協議SMTP,它們可能使用IMAPPOP3。 在您需要更改由他們所使用的協議,即引用屬性的話: props.put("mail.pop3.socketFactory.port", "25"); props.put("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");

您正在使用getDefaultInstance的會話,但如果有一個會話已經建立,它將返回現有會話和你的財產將會丟失。

檢查常見的錯誤頁面,它擁有一切: https://javaee.github.io/javamail/FAQ#commonmistakes

+0

tried..but沒有luck..same錯誤.. – next2u

+0

嘗試添加屬性:'mail.smtp.ssl.enable = TRUE; –

相關問題