Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add support for OAUTH2 authentication #421

Closed
chris-cynation opened this issue Sep 27, 2022 · 5 comments
Closed

Enhancement: Add support for OAUTH2 authentication #421

chris-cynation opened this issue Sep 27, 2022 · 5 comments

Comments

@chris-cynation
Copy link

Hi,

I was wondering what OAuth2 support there is and if there are plans to support it if not.

@bbottema
Copy link
Owner

OAuth2... how? This is an SMTP library, it connects and authenticates with SMTP servers. I'm not sure where OAth2 comes into the picture here.

@neoxpert
Copy link

neoxpert commented Jan 4, 2023

I guess this might be important as it is now required when connecting /authenticating to smtp.office365.com if not changes for a mailbox (which is not always possible, especially if a customer does not want to pass on the actuall password to external solutions in the future). At least the server response states 250-AUTH LOGIN XOAUTH2 and plain login using username and password does not work anymore. Also gmail does accept OAuth2 authentication.

So the question would be how to achieve this type of authentication with simple-java-mail assuming that there is a working OAuth2 token at hand.

@bbottema
Copy link
Owner

bbottema commented Jan 4, 2023

Interesting! Apparently, this was already possible in Sun JavaMail.

First of all, to start testing this in order to develop this feature, there are a few steps to get stuff working (took a while to figure this out):

  1. Create a new Google API Project with new consent screen and OAuth client credentials, as described at github.com/nfo/gmail_xoauth.
  2. Now generate a new OAuth2 token using this new Google API and to do that you need an app that will invoke the consent screen in the browser for the actual login. Unfortunately the oauth2.py Python script from that repo is old and broken, but luckily you can find new working instructions here, which uses a newer Python script from getmail6.
  3. Then with this OAth2 token string, go to yet another project, gmail_xoauth, and open the Java oauth2 module, configure Sun JavaMail dependencies and all and run OAuth2Authenticator.java with the IMAP commented out in the static void main. With a valid OAuth2 token this should run and finish without errors.

bbottema added a commit to simple-java-mail/smtp-connection-pool that referenced this issue Jan 4, 2023
… add your own OAuth2 security provider though, Simple Java Mail will use Google's reference implementation from https://github.com/google/gmail-oauth2-tools)
bbottema added a commit that referenced this issue Jan 5, 2023
…mentation. Code double check and junit tests still pending...
@bbottema bbottema added this to the 7.6.0 milestone Jan 5, 2023
bbottema added a commit that referenced this issue Jan 5, 2023
…mentation. Code double check and junit tests still pending...
@bbottema
Copy link
Owner

bbottema commented Jan 5, 2023

I just discovered JavaMail / Jakarta Mail already supported it out of the box, without using Google's version.

Guess I'll have to start over :|

@bbottema bbottema changed the title OAuth2 Support Enhancement: Add support for OAUTH2 authentication Jan 5, 2023
@bbottema
Copy link
Owner

bbottema commented Jan 5, 2023

OAUTH2 authentication is now supported with the release of 7.6.0!

It's super simple, just use TransportStrategy.OAUTH2 (which is based on TransportStrategy.SMTP_TLS) and use your OAuth2 token as password.

MailerBuilder
  .withSMTPServer("server host", 587, "username", yourOAuth2Token)
  .withTransportStrategy(TransportStrategy.SMTP_OAUTH2)
  .buildMailer()
  .sendMail(email);

Of course, the real challenge now is how are you going to get that OAuth2 token? That varies from platform to platform.

@bbottema bbottema closed this as completed Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants