我使用以下 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