【发布时间】:2015-08-10 05:11:57
【问题描述】:
我正在尝试向一个电子邮件帐户 (gmail) 发送电子邮件,我已经为我不在家时使用 javamail 设置了动态提醒,但是我使用的所有示例代码都可以正确编译,但是当我使用时给我一个错误尝试运行代码(javax.mail.AuthenticationFailedException)。这里我提供了一段我目前正在使用的代码。
public static void main(String[] args) {
final String username = "username@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", "587");
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("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Sample Subject");
message.setText("Sample body");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
【问题讨论】:
标签: java email jakarta-mail send