JavaMail with OAuth return "A1 NO AUTHENTICATE failed"

Patric Dosch 5 Reputation points
2023-06-20T15:25:55.5533333+00:00

Hello together,

we want to write an application where we proccess incoming mails from a mailbox in the background, for that we want to read the inbox of a user with a Java application via IMAP and OAuth (as Basic Auth is disabled).

I have found so many articles dealing with this topic and issue, but unfortunately every attempt we have made has not worked. Therefore, I try it now here!

What do we currently have?

What do we currently have?

App:

Our Service Provider registered an app (got a ClientId, Secret and TenantId) with permissions. For example, we have already customized this according to https://video2.skills-academy.com/en-us/answers/questions/1230605/a1-no-authenticate-failed-using-javamail-and-oauth as well (sorry for the german screenshot).

app-permissions

Access Token:

The AccessToken that we are getting looks ok to me, at least it knows what right my app has.

// Set up MSAL authentication context

Debugged with https://jwt.ms/

roles

Java Mail:

We are using jakarta.mail 2.1.2, implementation from org.eclipse.angus (but the good old javax.mail is the same).

This is for sure a bit more complicated since there are so many variants of the properties. For example, we have also defined imaps as store (as you see below), or tried sasl also "mail.imaps.sasl.mechanisms". But in my opinion the example in the linked article would be sufficient. It makes no different if we are using "imap" store or using the getInstance with Authenticator to provide user and token. So, currently we have.

Properties props = new Properties();
    props.put("mail.store.protocol", "imaps");
    props.put("mail.imaps.host", "outlook.office365.com");
    props.put("mail.imaps.port", "993");
    props.put("mail.imaps.ssl.enable", "true");
    props.put("mail.imaps.auth", "true");
    props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
    props.put("mail.imaps.user", USER);
    props.put("mail.debug", "true");
    props.put("mail.debug.auth", "true");

    Session session = Session.getInstance(props);
    Store store = session.getStore();
    store.connect(USER, accessToken);

Debug results in console:

DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [******]
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=meeting-reply-test@themis-wissen.de, password=<non-null>
A1 AUTHENTICATE XOAUTH2 ******
A1 NO AUTHENTICATE failed.

So we're at a bit of a loss here and pretty clueless. Is there anyone here who can help us?

I look forward to hearing from you.

Patric

ps. If you have any questions, I can provide more info. A call, or whatever is needed.

ps. If I have written something totally stupid, forgiven, I am at least regarding the Microsoft technologies a little beginner.

Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
540 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,374 questions
{count} vote

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.