3

Can someone give me aguide to authenticate to office 365 exchange with javax mail with oAuth2?

transport.connect(mailConfig.getMailUsername(), mailConfig.getMailPassword());

(I Tried also with Bearer + ...

Properties props = new Properties();

            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.host", "smtp.office365.com");
            props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.debug.auth", "true");
            props.put("mail.debug", "true");
            Session session = Session.getInstance(props);
            session.setDebug(true);
            Transport transport = session.getTransport("smtp");
            transport.connect(mailConfig.getMailUsername(), mailConfig.getMailPassword());

Photo of Permission for Microsoft Azure

Photo of Permission for Microsoft Azure

Here is my stackstrace:

DEBUG SMTP: Attempt to authenticate using mechanisms: XOAUTH2
DEBUG SMTP: Using mechanism XOAUTH2
AUTH XOAUTH2 [...]
535 5.7.3 Authentication unsuccessful [ZR0P278CA0118.CHEP278.PROD.OUTLOOK.COM]
2
  • The password is your access token right?
    – Zveratko
    Jun 16, 2022 at 6:25
  • I have currently the same problem The only thing im doing differently is using the access_token i have got from login.microsoftonline.com/{tenantId}/oauth2/v2.0/token (using client_credential grant) instead of the password. The result is the exact same. I guess there is a property, where we have to put the token in, because it does'nt meter what i use as the password parameter, it always fails. Jun 21, 2022 at 15:46

4 Answers 4

3

Using the client credentials flow for SMTP Authentication is currently not supported.

See the NOTE in Microsoft's doc on the SMTP Protocol Exchange.

Also, see this post from the Exchange Team Blog. They just added support for IMAP and POP in June 2022. SMTP is not yet there and they have not shared a timeline for when they expect to have it. They report that they will continue to support basic authentication (username/password) for SMTP.

0

I think you need to use

props.put("mail.smtp.sasl.mechanisms.oauth2.oauthToken", accessToken);

and then use null as a password

transport.connect("smtp.office365.com", mailConfig.getMailUsername(), null);

For me this was working

DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
220 AS9PR04CA0130.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 29 Jun 2022 10:36:34 +0000
DEBUG SMTP: connected to host "smtp.office365.com", port: 587
3
  • The connection is no problem for me, but the actual sending fails: 530 5.7.57 Client not authenticated to send mail. [FR3P281CA0013.DEUP281.PROD.OUTLOOK.COM] DEBUG SMTP: got response code 530, with response: 530 5.7.57 Client not authenticated to send mail. [FR3P281CA0013.DEUP281.PROD.OUTLOOK.COM] RSET DEBUG SMTP: EOF: [EOF] DEBUG SMTP: MessagingException while sending, THROW: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 Client not authenticated to send mail. [FR3P281CA0013.DEUP281.PROD.OUTLOOK.COM] Jul 8, 2022 at 9:28
  • Have you made any progress on this? I'm having the exact same issue. One thing I'm seeing in places is that it's not possible to use client_credential flow for SMTP Auth. However this is not very clear from the documentation.
    – Chazmus
    Aug 3, 2022 at 14:40
  • 1
    As of 12th Sept 2022, SASL XOAUTH2 client credentials flow for SMTP is not supported. See this: learn.microsoft.com/en-us/exchange/client-developer/… Sep 12, 2022 at 11:37
0

As of 10-Nov-2022, the SMTP client credential flow is still not supported. They announced support for POP and IMAP but I saw no support for SMTP so I submitted a question to the Microsoft Community Hub since I am experiencing the same issue. I was told by Greg Taylor, who is part of the EXCHANGE Microsoft team, that client credential flow is not yet possible with SMTP. He said they will announce when it's available, the plan/hope is that it will be by the end of the year. See this link for my question and the response https://techcommunity.microsoft.com/t5/exchange-team-blog/announcing-oauth-2-0-client-credentials-flow-support-for-pop-and/ba-p/3562963

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 14, 2022 at 12:40
0

here are the properties that worked for me, assuming you have SMTP AUTH enabled on your O365 account and have successfully negotiated the token with scope "https://outlook.office365.com/SMTP.Send offline_access"



    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.host", "smtp.office365.com");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");
    props.put("mail.smtp.auth", "true");

And then contrary to other advice I've read, pass your accessToken in instead of the password when calling Transport.connect(), and JavaMail will take care of formatting it:



    Session session = Session.getInstance(props);
    try {
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.office365.com", emailAddress, accessToken);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }   

Here's the resulting conversation:


    DEBUG SMTP: Attempt to authenticate using mechanisms: XOAUTH2
    DEBUG SMTP: Using mechanism XOAUTH2
    AUTH XOAUTH2 (LongStringOfAlphaNumerics)
    235 2.7.0 Authentication successful

Best of luck, hopefully this will save someone some time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.