当我需要在特定时间发送邮件时,我目前正在使用Spring Boot应用程序,因此我使用 javax.mail API进行了此操作,该程序在本地运行良好机器,但是当我在Google Cloud App Engine中部署应用程序时,我不断收到以下错误:
javax.mail.AuthenticationFailedException
关于邮寄要求,我尝试按照Google Cloud文档的说明进行操作,但我一直遇到相同的错误。
遵循我的代码。*
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帐户上启用了安全性较低的应用程序身份验证选项,但它一直拒绝身份验证尝试。
谢谢。
答案 0 :(得分:0)
我一直在尝试多种配置以使其正常运行,但没有成功,因此我切换到另一个选项,我实施了 sendGrid 解决方案, google cloud 将其列出为一种可能的替代方法,它是如此简单,如此安全,并且从第一次尝试开始就起作用。
感谢您的建议
答案 1 :(得分:0)