【发布时间】:2013-12-11 18:38:16
【问题描述】:
我一直在开发一个程序来从基于桌面的 Java 应用程序发送电子邮件。
当我使用 Google SMTP 服务器 (smtp.gmail.com) 测试它时,该程序运行良好,但是当我为其他 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 中运行代码。
下面是代码sn-p -
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("qwert@xyzcompany.com","*******");
}
});
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("qwert@xyzcompany.com"));
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 服务器,那么它可能不是代码语法。检查您是否可以从您正在运行的机器、有效的电子邮件帐户、正确的端口等访问...
-
所有的道具都正确吗?由于它适用于谷歌,我认为你的问题是你有一个错误的端口或类似的东西。
标签: java email smtp jakarta-mail smtpclient