Spring boot deployed to war doesnt send google emails

Question

I have a spring boot app which when I run I can succesfully send email messages, however when I deploy it to war on my AWS server it suddenly cant send emails and throws me error:

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

Code:

@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");
}

properties

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

Credential are okay and I have enabled less secured apps in my account settings. I don't know whats the problem.


Show source
| java   | email   | spring   | spring-boot   | gmail   2017-01-07 19:01 1 Answers

Answers ( 1 )

  1. 2017-01-07 19:01

    I found the solution. Problem was not with spring. It's just sometimes "allowing access for less secured apps" isn't enough.

    Here are some more informations why google may won't allow access. https://support.google.com/accounts/answer/6009563

    First tip is to:

    1. go to: https://g.co/allowaccess
    2. allow login from different device.
    3. use different device to login.

    And this is what helped in my case.

    All credits to: http://serverfault.com/questions/635139/how-to-fix-send-mail-authorization-failed-534-5-7-14

◀ Go back