Gmail SMTP works in SpringBoot but not on Tomee Server

169 Views Asked by At

I am using exactly the same settings for javax Mail session, which works like charm in SpringBoot App but fails in Tomee Server.

On SpringBoot's application.properties, I have the following settings (which works):

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<my gmail>
spring.mail.password=<App PW generated on Google>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

And here are the setting in Tomee's conf/tomee.xml settings

<Resource id="mail/bjm" type="javax.mail.Session">
        mail.smtp.host=smtp.gmail.com
        mail.smtp.starttls.enable=true
        mail.smtp.port=587
        mail.transport.protocol=smtp
        mail.smtp.auth=true
        mail.smtp.user=<my gmail>
        password=<App PW generated on Google>
    </Resource>

On Tomee, when I run the application, I get the following error message:

 failure (javax.mail.AuthenticationFailedException: null)

I am puzzled what wrong am I doing in tomee.xml because I have followed the guidelines from here: https://tomee.apache.org/master/docs/configuring-javamail.html

How can I fix the issue?

1

There are 1 best solutions below

0
On

The sample you "state" you are following Declaring a JavaMail Resource which you aren't really since you have changed it.

Is for use with MAP SASL XOAUTH2 mechanism configuration Xoauth2 is a form of oauth2 that is supported by googles imap server.

rather then following the sample you have changed it to use user name and an apps password. This is not Xoauth2.

Offending lines of code:

spring.mail.username=<my gmail>
spring.mail.password=<App PW generated on Google>

You are getting a fail for javax.mail.AuthenticationFailedException because you have not added a valid access token as shown in the sample, which you have not shown in your code.

store.connect("imap.gmail.com", "<username>@gmail.com", "<YourAccesToken>");

To be 100% clear an apps password is not an access token an access token must be created by standard Oauth2 mechanizes of requesting authorization of the user, with a mail scope.