Skip to content

Enhancement: Add support for OAUTH2 authentication #421

Closed

Description

@chris-cynation

Hi,

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

Activity

  1. bbottema commented on Dec 25, 2022

    @bbottema
    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.

  2. neoxpert commented on Jan 4, 2023

    @neoxpert

    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.

  3. bbottema commented on Jan 4, 2023

    @bbottema
    Owner

    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.
  4. added 2 commits that reference this issue on Jan 4, 2023
  5. added this to the 7.6.0 milestone on Jan 5, 2023
  6. added a commit that references this issue on Jan 5, 2023
  7. bbottema commented on Jan 5, 2023

    @bbottema
    Owner

    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 :|

  8. changed the title [-]OAuth2 Support[/-] [+]Enhancement: Add support for OAUTH2 authentication[/+] on Jan 5, 2023
  9. bbottema commented on Jan 5, 2023

    @bbottema
    Owner

    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.

  10. venkatesh-i2i commented on Jul 25, 2024

    @venkatesh-i2i

    Hi @bbottema i am trying to use the TransportStrategy.SMTP_OAUTH2
    i am gmail oauth and only the send scope is enabled

    i getting Exception saying
    Caused by: jakarta.mail.AuthenticationFailedException: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
    ;
    nested exception is:
    jakarta.mail.AuthenticationFailedException: OAUTH2 asked for more
    Could you help if possible

  11. bbottema commented on Aug 26, 2026

    @bbottema
    Owner

    Current 9.x usage for a long-lived mailer (verified against 9.3.2):

    OAuth2AccessTokenProvider accessTokens = tokenService::currentAccessToken;
    
    Mailer mailer = MailerBuilder
        .withSMTPServer("smtp.example.com", 587, "user@example.com")
        .withTransportStrategy(TransportStrategy.SMTP_OAUTH2)
        .withOAuth2AccessTokenProvider(accessTokens)
        .buildMailer();

    Since 9.2, the refresh-aware provider is preferred for long-lived mailers because it is consulted before a physical SMTP connection is opened or reopened. Passing a fixed token as the SMTP password remains valid for a short-lived mailer. See the 9.2.0 release notes and current OAuth2 documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions