Java code for reading emails using IMAP with Oauth2 errors with "A1 NO AUTHENTICATE failed"

Vishnu Piskala 0 Reputation points
2023-06-08T19:00:38.7133333+00:00

We need to read emails for a batch process using OAUTH2 with IMAP in our Java Code. We followed the instructions from here: https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth.

We are now getting this error : A1 NO AUTHENTICATE failed.

The java code I followed is from here : https://ralph.blog.imixs.com/2022/10/24/how-to-access-outlook-office365-com-imap-form-java-with-oauth2/

Below are the following things I did:

  • Registered the application
  • Gave the permission "IMAP.AccessAsApp" in API Permissions under "Office 365 Exchange Online (1)"
  • Granted admin consent in API Permissions page.
  • Generated the client secret
  • Used the client secret and Obtained the access token as per the java code in the above link.
  • Registered service principals in Exchange by running the following commands in my windows powershell:

Install-Module -Name ExchangeOnlineManagement

Import-module ExchangeOnlineManagement

Install-Module -Name ExchangeOnlineManagement <organization id>

    New-ServicePrincipal -AppId <Application id> -ServiceId <serviceid> -Organization <org id>
  • Added mailbox permission using the following command in powershell:

Add-MailboxPermission -Identity "useremaild" -User <serviceid> -AccessRights FullAccess

This is what I'm trying to achieve: https://techcommunity.microsoft.com/t5/exchange-team-blog/announcing-oauth-2-0-client-credentials-flow-support-for-pop-and/ba-p/3562963

Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Dillon Silzer 57,826 Reputation points Volunteer Moderator
    2023-06-08T20:56:51.8566667+00:00

    Hello Vishnu,

    Can you try adding the following permissions to the registered app?

    User's image

    (cited from https://learn.microsoft.com/en-us/answers/questions/1230605/a1-no-authenticate-failed-using-javamail-and-oauth)

    Perhaps you are missing some permissions and this is causing issue when trying to authenticate.


    If this is helpful please accept answer.


  2. Krishnaroopa Senthilkumar 0 Reputation points
    2024-01-04T13:23:26.02+00:00

    is the issue resolved

    We are getting the same error

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.