我使用以下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,从那里我可以连接
异常(exception):
java.lang.RuntimeException:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465,响应:-1
最佳答案
我正在使用端口587
并且我能够发送/接收邮件..或者您可以设置属性
mail.smtp.timeout改为25000,试试是否是超时异常..
关于java - 无法连接到 smtp.gmail.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629153/