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);
}
}
}
如果完全相同的代碼與另一個SMTP服務器一起工作,那麼它可能不是代碼語法。檢查你是否有從你運行的機器,有效的電子郵件帳戶,正確的端口等訪問... – Bizmarck
所有的道具是正確的?因爲它適用於谷歌,我認爲你的問題是你有一個錯誤的端口或類似的東西。 – Blub