How to access shared mailbox using OAuth 2 and Jakarta Mail with IMAP

MXDHC 5 Reputation points
2023-03-21T12:19:03.59+00:00

I am trying to access and read shared mailbox, for that I am using Jakarta Mail version 2.0.1. Also, as Microsoft has already stopped supporting basic authentication so I have also implemented OAuth 2 code authorization flow and getting correct access token.

 

Now by using below code I am trying to access and read shared mail box:

void testSharedMailBox() throws MessagingException {
        Properties props = new Properties();
        props.put("mail.imaps.starttls.enable", "true");
        props.put("mail.imaps.ssl.enable", "true");
        props.put("mail.imaps.auth", "true");
        props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
        props.put("mail.imaps.auth.login.disable", "true");
        props.put("mail.imaps.auth.plain.disable", "true");
        props.put("mail.imaps.auth.ntlm.disable", "true");
        props.put("mail.imaps.auth.gssapi.disable", "true");

        props.put("mail.imaps.host", "outlook.office365.com");
        props.put("mail.imaps.port", 993);
        props.put("mail.debug.auth", "true");
        Session session = Session.getInstance(props);
        session.setDebug(true);
        Store _store = session.getStore("imaps");


        _store.connect("outlook.office365.com", 993, "user_email\\noreply_alias",
                "valid_access_code_here");

        // also tried below but didn't work
        // _store.connect("outlook.office365.com", 993, "user_email\\shared_mailbox_email",
                "valid_access_code_here");

        Folder inbox = _store.getFolder("INBOX");
        // inbox.open(Folder.READ_ONLY);
        int messageCount = inbox.getMessageCount();
        System.out.println(messageCount);


        _store.close();
    }

After reading multiple questions on forums it seems that to access shared mail box usernmae should be <user_email>\shared_box_alias. As password I am setting OAuth access code received after authenticating user with his email address.

 

The above code is failing with exception:

AUTHENTICATE failed.
jakarta.mail.AuthenticationFailedException: AUTHENTICATE failed.
    at app//com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:708)
    at app//jakarta.mail.Service.connect(Service.java:342)
    at app//AccountTest.testSharedMailBox(AccountTest.java:48)
    at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base@11.0.11/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base@11.0.11/java.lang.reflect.Method.invoke(Method.java:566)
    at app//org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
    ... <truncated more stack trace>

Below is the Jakarta Mail debug log:

DEBUG: setDebug: Jakarta Mail version 2.0.1
DEBUG: getProvider() returning jakarta.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
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: enable STARTTLS
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. [some_lomg_string_removed_for_privacy]
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=AdeleV@tm6gs.onmicrosoft.com\noreply_alias, password=<non-null>
A1 AUTHENTICATE XOAUTH2 some_lomg_string_removed_for_privacy
A1 NO AUTHENTICATE failed.
Microsoft Authenticator
Microsoft Authenticator
A Microsoft app for iOS and Android devices that enables authentication with two-factor verification, phone sign-in, and code generation.
7,365 questions
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.
556 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.