目前,当需要在特定时刻发送邮件时,我正在开发一个spring引导应用程序,因此我使用了javax.mail api进行此操作,该程序在本地机器上运行良好,但是当我在google应用程序引擎中部署应用程序时,我一直收到以下错误:
javax.mail.AuthenticationFailedException
当涉及到邮件需求时,我试着遵循google云文档说明,但我一直收到同样的错误。
遵循我的代码*
public boolean sendMail(String indicator, String destination, String hotelName, String username, String password) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(env.getProperty(IConstants.mailAddress),
env.getProperty(IConstants.mailPass));
}
});
String indic = null;
if(indicator.equals("R")){
indic = "RECEPTIONIST";
}
if(indicator.equals("M")){
indic = "ROOMMAID";
}
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(env.getProperty(IConstants.mailAddress)));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination));
message.setSubject(indic + "Hotel " + hotelName + " - Credentials");
message.setText("Hello " + hotelName
+ " Employee, Down Below Your Credentials for a secure access to your workspace " + ".\n USERNAME: "
+ username + " PASSWORD: " + password + ".\n" + "Greetings.");
Transport.send(message);
return true;
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}发件人邮件在授权邮件发送者列表中,所有内容都正确配置,应用程序响应所有请求,但在发送邮件步骤中始终失败。
我在发件人gmail帐户上启用了安全性较低的应用程序身份验证选项,但它一直拒绝身份验证尝试。
谢谢。
发布于 2019-03-05 13:37:35
我不断尝试多个配置来使其工作,但没有,所以我转向了另一个选项,我实现了sendGrid解决方案,google 将其列为一种可能的替代方法,它是如此简单,如此安全,而且从第一次尝试就起了作用。
谢谢你的建议
发布于 2019-03-05 14:28:54
您是使用标准env还是灵活env。在我们的例子中,我们在google邮件中使用了灵活的env。对我们来说很好。我怀疑,在你的情况下,问题是第三方访问检查谷歌邮件帐户。启用此选项后,尝试相同的smtp,以便应用程序能够访问gmail smtp。
打开从smtp中使用的帐户屏幕截图中显示的设置的访问权限。

尽管sendgrid是一个很好的邮件服务。但我们更喜欢谷歌smtp。
https://stackoverflow.com/questions/54990624
复制相似问题