【发布时间】:2019-09-29 01:38:58
【问题描述】:
我正在尝试使用 javax.mail 使用 gmail smtp 发送电子邮件。以下是我的代码
public static void send(String from,String password,String to,String sub,String msg){
//Get properties object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
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");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
代码运行良好当我在本地服务器上运行它但是当我尝试在 Elastic beanstalk 上运行它(我的服务器在 AWS EBS 上运行)时,身份验证失败异常即将到来 注意:我已经从 Google A/c Setting 开启了对不太安全的应用程序的访问,但我仍然收到此错误
javax.mail.AuthenticationFailedException: 534-5.7.14 请通过您的网络浏览器登录,然后重试。 534-5.7.14 了解更多信息 534 5.7.14 https://support.google.com/mail/answer/78754l13sm3053341iti.6 - gsmtp
【问题讨论】:
标签: java gmail gmail-api amazon-elastic-beanstalk