【发布时间】:2013-09-05 06:25:24
【问题描述】:
我使用以下 java 程序从 gmail 帐户发送邮件
final String username = "user@gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
Session session =
Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("user@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("user@live.in"));
message.setSubject("Testing Subject");
message.setText("Dear Bhavik Patel," +
"\n\n This is just a mail!");
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
throw new RuntimeException(e);
}
我也试过 587 端口,但它不工作
Transport.send(message);
在这个执行过程中尝试连接和发送
我不知道它有什么问题。我也尝试过 telnet,从那里我可以连接
例外:
java.lang.RuntimeException:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465,响应:-1
【问题讨论】:
-
您如何确定“它不起作用”?错误信息,超时,异常,什么?
-
SSL属性(用于端口 465)略有不同。查看this example了解更多详情(注意,我没有尝试过) -
超时 6 分钟后抛出
-
@MadProgrammer 让我试试你链接中的第二个例子
-
双 ping,没有问题(SSL 和 TLS)。你用的是什么版本的javamail?我使用的是 1.4.7 和 1.5.0
标签: java jakarta-mail