I have a java client using javamail and google example code to connect to gmail IMAP server using a client secret and OAuth token. The issue is that about 1/3 of the time, the com.sun.mail.imap.IMAPStore.protocolConnect call fails with an AuthenticationFailedException: Invalid Credentials (Failure) exception. In the log there are the following lines for each failure:
DEBUG IMAPS: SASL client XOAUTH2
DEBUG IMAPS: SASL callback length: 1
DEBUG IMAPS: SASL callback 0: javax.security.auth.callback.NameCallback@12cdcf4
DEBUG IMAPS: SASL no response
2023-01-04 08:45:55 WARN ImapStoreFactory.getImapStore.140 - Failed to authenticate to 74.125.142.108 after 5 attempts.
(I have a retry loop that backs off after each try up to 5 tries to see if that would help.)
The (partial) stack trace is:
javax.mail.AuthenticationFailedException: Authentication failed to 74.125.142.108 after 5 attempts.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:732)
at javax.mail.Service.connect(Service.java:366)
at com.google.code.samples.oauth2.OAuth2Authenticator.connectToImap(OAuth2Authenticator.java:91)
The issue is the SASL no response from the server.
This same code, with the same credentials, works all the other times on the first connection attempt, including to the same IP address, both before and after the failed attempts.
imap.gmail.com resolves to (at least?) 24 different IP addresses that I have seen, and they are all open and accessible from the client machine.
I have tried connecting to a known IP address directly (as opposed to connecting to imap.gmail.com) but then I get a HTTPS certificate failure.
Any ideas as to why this is happening, or how to mitigate or work around would be appreciated.
Thanks!
- Linus