Search code examples
javaspring-bootsmtpgmail

Why my email sending doesn't work on deployed website but it works on my localhost?


Until some weeks, my email would send perfectly from the website I have created for a cousin's business, but now she told me her customers get this error every time they try to make an appointment.

Now I have started to debug it, I've checked that the gmail I am using for sending is allowing every application to access it, I have checked again the spring boot application properties, etc. When testing it on my localhost it sends the email, but on the deployed website it gives the error in the picture below... Again, two months ago when I did the last check, it worked fine. And it still works fine, but just on my localhost.

This is how my application properties looks like:

spring.mail.host=smtp.gmail.com
spring.mail.username=..
spring.mail.password=..
spring.mail.port=587
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

And this is a screenshot with the error everyone gets (the webiste is deployed with heroku):the thing

Thank you!


Solution

  • As the error message states, there's an AuthenticationException and the error message is asking to log in through the browser.

    Since 2FA is off, the most likely reason is that they want to show a CAPTCHA, which would need human interaction. The link I found suggests that by visiting https://accounts.google.com/DisplayUnlockCaptcha on the target account would allow you to clear the captcha manually, and then it would work.

    However I'm not convinced that clearing the captcha manually is enough, since it might come back at any time. It seems the proper way to go would be through enabling 2FA, and using application specific passwords.