JavaMail cannot log in to Office 365 IMAP with an OAuth token
A01 NO AUTHENTICATE failed.Short answer
Exchange Online only accepts an OAuth access token over the XOAUTH2 mechanism, and Jakarta Mail has that mechanism switched off by default. After that, the failure is nearly always the token: wrong scope, or for app-only access a missing Exchange service principal and mailbox permission.
Fix it fast
1Find out which cause you have
Turn on protocol debug output and look at which mechanisms the client tries. If XOAUTH2 never appears in the AUTHENTICATE line, the first cause applies. If it does appear and the server still answers NO, check the token.
props.put("mail.debug", "true");2Match what you see, then jump to its fix
- 1
The debug output does not show AUTHENTICATE XOAUTH2XOAUTH2 is not enabled in the mail sessionFix - 2
The token request succeeds, but the server answers AUTHENTICATE failedThe token was requested with the wrong scopeFix - 3
Token has the IMAP.AccessAsApp role but AUTHENTICATE failedApp-only (client credentials) access is missing its Exchange service principalFix
- 1
What does the error look like?
A01 NO AUTHENTICATE failed.AUTHENTICATE failed
Cause 1 of 3
XOAUTH2 is not enabled in the mail session
1Check
The IMAP provider keeps XOAUTH2 out of its default mechanism list, because the password must be an access token instead of a real password.
grep -rn "auth.mechanisms\|xoauth2" src/ | head2Fix
Set the mechanism list to XOAUTH2 for the IMAP protocol and pass the access token as the password.
Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true");
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("outlook.office365.com", 993, "user@contoso.com", accessToken);3Verify
The debug output now shows AUTHENTICATE XOAUTH2 and the server answers OK.
store.isConnected()Confirmed by eclipse-ee4j.github.io
Cause 2 of 3
The token was requested with the wrong scope
1Check
For a signed-in user, the scope has to be the full Outlook resource URL, not a Graph scope. Decode the token on a test tenant and read its scp claim.
echo "$ACCESS_TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null2Fix
Request https://outlook.office.com/IMAP.AccessAsUser.All (plus offline_access if you want refresh tokens) through the authorization code or device code flow.
scope=https://outlook.office.com/IMAP.AccessAsUser.All offline_access3Verify
Request a new token and connect again.
store.connect("outlook.office365.com", 993, "user@contoso.com", newAccessToken);Confirmed by learn.microsoft.com
Cause 3 of 3
App-only (client credentials) access is missing its Exchange service principal
1Check
Client credentials needs more than the Entra permission. A tenant admin must grant consent, register the service principal in Exchange, and give it access to each mailbox.
Get-ServicePrincipal | fl2Fix
Use the Object ID of the Enterprise application, not of the App registration, when you register the service principal, then grant mailbox access. Request the token with the scope https://outlook.office365.com/.default.
Connect-ExchangeOnline -Organization <tenantId>
New-ServicePrincipal -AppId <APPLICATION_ID> -ObjectId <ENTERPRISE_APP_OBJECT_ID>
Add-MailboxPermission -Identity "john.smith@contoso.com" -User <SERVICE_PRINCIPAL_ID> -AccessRights FullAccess3Verify
List the registered principals, then retry the IMAP login with a freshly issued token.
Get-ServicePrincipal | flConfirmed by learn.microsoft.com
How do I stop it coming back?
- Keep the token request and the IMAP login in one small test class, so a scope change is caught straight away.
- For app-only access, write down the three steps (consent, service principal, mailbox permission) in your runbook.
- Use the Enterprise application Object ID when registering the service principal.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Related fixes
Security
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fix