java - 部署到 war 的 Spring Boot 不发送谷歌电子邮件

标签 java spring email spring-boot gmail

我有一个 Spring Boot 应用程序,当我运行时,我可以成功发送电子邮件,但是当我将其部署到我的 AWS 服务器上时,它突然无法发送电子邮件并抛出错误:

org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsv
534-5.7.14 GyjozMVw6yK71QY1-533Cq9j3pFvZ4Qk6tvNi8jraY2Qr1olGNIenIvjiHO8PYS2clGWG_
534-5.7.14 AKcP6Ny-ZwLVxAyKeqgAnOqFPC-fC2Ha4fZuHlbiWsi8wYfeQzgoSjzBowz9R4uYWPocKY
534-5.7.14 yY7pG1k4O7ukaHccwVpLm8ypHX_MuWoiYbKFE6bSG5ht7Jps1MoPoWpE21oDtiu0udijNz
534-5.7.14 zwKkTw01RQUyNDlkJkjHWqkGQW1GM> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 s20sm9437528wmb.9 - gsmtp

代码:

@Value("${spring.mail.username}")
private String recipient;

@Autowired
private JavaMailSender javaMailSender;

public SimpleStatusObject sendEmail(SimpleEmail simpleEmail) throws MessagingException, UnsupportedEncodingException {
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);

    helper.setTo(recipient);
    helper.setFrom("MOJA_STRONA");
    helper.setReplyTo(simpleEmail.getSenderEmail(), simpleEmail.getSenderName());
    helper.setSubject(simpleEmail.getSubject());
    helper.setText(simpleEmail.getMessage());

    javaMailSender.send(message);
    return new SimpleStatusObject(Status.SUCCESS, "message sent");
}

属性

spring.mail.host = smtp.gmail.com
spring.mail.username=email@gmail.com
spring.mail.password=mypass
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false

凭据没问题,我在帐户设置中启用了安全性较低的应用程序。我不知道问题出在哪里。

最佳答案

我找到了解决方案。问题不在于 Spring 。只是有时“允许访问安全性较低的应用程序”还不够。

以下是 Google 可能不允许访问的更多信息。 https://support.google.com/accounts/answer/6009563

第一个提示是:

  1. 前往: https://g.co/allowaccess
  2. 允许从不同设备登录。
  3. 使用不同的设备登录。

这对我的案例有帮助。

所有积分均归于: https://serverfault.com/questions/635139/how-to-fix-send-mail-authorization-failed-534-5-7-14

关于java - 部署到 war 的 Spring Boot 不发送谷歌电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524684/

相关文章:

java - 递归装箱算法无法扩展

java JLabel 在 90 度旋转后无法正确重新绘制调整大小的标签

java - Spring 安全 i18n : wrong error message resolved

Java 客户端调用 jar 文件中的算法,该文件是 Java Spring 中 Web 界面的一部分

email - 避免 webmail gmail 中的浏览器图像缓存

javascript - 为数组中的每个名称查找 Meteor 中另一个集合中的一些用户数据

java - 使用图像序列和数组绘制动画?

java - 为什么 ClassCastException 是运行时错误?

java - 如果数据中有一些特殊符号,则从数据库中检索数据包含一些字符

email - 电子邮件主题是否需要转义为HTML?