Java OAuth 2.0 Application Authentication for Outlook 355

Andy Brunner 1 Reputation point
2021-02-21T11:12:11.453+00:00

I have a Java program which tries to use the Eclipse Jakarta APIs to connect with OAuth 2.0 to Outlook 365. Here is what I did:

Azure portal:

  • Created a new App registration with type "Accounts in any organization and personal Microsoft account"
  • Added API permission: Microsoft Graph > Delegated permission "email" and "IMAP.AccessAlsUser.All" / "POP.AccessAsUser.All"
  • Created a new client secret

Java Program:

  • Changed a working application to use OAuth 2.0 authentication
  • Changed to send the "Client Secret Value" instead of the normal password

Debug Output:

Mail property mail.imaps.auth.plain.disable: true
Mail property mail.imaps.auth.mechanisms: XOAUTH2
Mail property mail.imaps.port: 993
Mail property mail.store.protocol: imaps
Mail property mail.imaps.partialfetch: false
Mail property mail.mime.decodetext.strict: false
Mail property mail.imaps.peek: true
Mail property mail.mime.charset: UTF-8

DEBUG: setDebug: Jakarta Mail version 2.0.0
DEBUG: getProvider() returning jakarta.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.partialfetch: false
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: peek
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [WgBSADAAUAAyADcAOABDAEEAMAAxADAAMwAuAEMASABFAFAAMgA3ADgALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: protocolConnect login, host=outlook.office365.com, user=xxx@outlook.com, password=<non-null>
DEBUG IMAPS: AUTHENTICATE XOAUTH2 command trace suppressed
DEBUG IMAPS: AUTHENTICATE XOAUTH2 command result: A1 NO AUTHENTICATE failed.

How can I find out, what is missing or what I did wrong?

Thanks!

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,299 questions
{count} votes