2013-11-26 193 views
1

我一直在開發一個程序來從基於桌面的Java應用程序發送電子郵件。當我與谷歌SMTP服務器(smtp.gmail.com)測試它Java SMTP郵件 - 連接超時錯誤

程序工作正常,但是當我測試了其他的SMTP服務器會生成一個錯誤 -

javax.mail.MessagingException: Could not connect to SMTP host: 
smtp.collaborationhost.net, port: 465; 
nested exception is: 
java.net.ConnectException: Connection timed out: connect 

我在Eclipse中運行代碼。

下面的代碼片段 -

public class Emailer { 

    public static void main(String[] args) { 

     String name = "Testing"; 

     Properties props = new Properties(); 

     props.put("mail.smtp.host", "smtp.collaborationhost.net"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 

     Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("[email protected]","*******"); 
       } 
     }); 

     try 
     { 
      Message message = new MimeMessage(session); 

      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("testgmail.com"));  
      message.setSubject("Generic Mail Test from Eclipse !!!"); 
      message.setText("This is a Test Mail, sent from Local Eclipse System via Google SMTP server. \n\n" + "Regards, \n" + name + "."); 
      Transport.send(message); 

      System.out.println("Message Sent !!!"); 
     } 

     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
    } 
} 
+0

如果完全相同的代碼與另一個SMTP服務器一起工作,那麼它可能不是代碼語法。檢查你是否有從你運行的機器,有效的電子郵件帳戶,正確的端口等訪問... – Bizmarck

+0

所有的道具是正確的?因爲它適用於谷歌,我認爲你的問題是你有一個錯誤的端口或類似的東西。 – Blub

回答

0

這JavaMail的FAQ條目會幫助你debug connection problems。在這種情況下,我懷疑其他服務器沒有使用與Gmail相同的端口,因此您需要更改您的配置。

此外,您可以通過更正這些common mistakes中的一些來簡化您的程序。

相關問題