Sample code for 30+ languages & platforms
C Requires Chilkat v11.0.0+

Microsoft 365 IMAP: OAuth2 Login and Read-Only Inbox

See more IMAP Examples

Authenticate to outlook.office365.com:993 using XOAUTH2, then EXAMINE the Inbox without changing read flags. This is Exchange Online work/school access: the mailbox must permit IMAP and the app must satisfy tenant consent and access policy. Basic authentication and app passwords cannot restore removed IMAP Basic authentication.

Personal Outlook.com uses the same IMAP server, but requires personal-account delegated authorization and IMAP enabled in Outlook.com settings. To use it here, select that mailbox address and load qa_data/tokens/hotmail.json from the personal-account PKCE example. Application-only client credentials are an Exchange Online organizational scenario, not personal Outlook.com authorization.

First authorize the account with https://outlook.office.com/IMAP.AccessAsUser.All and offline_access, then refresh the access token when needed. This example reads qa_data/tokens/office365.json from a private working directory. Microsoft Graph permissions such as Mail.Send or Mail.Read do not authorize these mail protocols. Do not print tokens or authentication logs.

Microsoft reference documentation

Chilkat C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkImap.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject token;
    const char *mailbox;
    HCkImap imap;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(token,"qa_data/tokens/office365.json");
    if (success != TRUE) {
        printf("Could not read qa_data/tokens/office365.json from the private working directory.\n");
        CkJsonObject_Dispose(token);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(token,"access_token") != 1) || (CkJsonObject_StringOfEquals(token,"access_token","",TRUE) == TRUE)) {
        printf("An OAuth access token is required. Authorize or refresh the account first.\n");
        CkJsonObject_Dispose(token);
        return;
    }

    mailbox = "user@example.com";
    imap = CkImap_Create();
    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);
    CkImap_putRequireSslCertVerify(imap,TRUE);
    CkImap_putAuthMethod(imap,"XOAUTH2");
    success = CkImap_Connect(imap,"outlook.office365.com");
    if (success != TRUE) {
        printf("Could not establish the Microsoft IMAP TLS connection.\n");
        CkJsonObject_Dispose(token);
        CkImap_Dispose(imap);
        return;
    }

    success = CkImap_Login(imap,mailbox,CkJsonObject_stringOf(token,"access_token"));
    if (success != TRUE) {
        CkImap_Disconnect(imap);
        printf("IMAP authentication failed. Check the mailbox, token scope, expiry, and account policy.\n");
        CkJsonObject_Dispose(token);
        CkImap_Dispose(imap);
        return;
    }

    //  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
    success = CkImap_ExamineMailbox(imap,"INBOX");
    if (success != TRUE) {
        CkImap_Logout(imap);
        CkImap_Disconnect(imap);
        printf("Could not examine INBOX.\n");
        CkJsonObject_Dispose(token);
        CkImap_Dispose(imap);
        return;
    }

    printf("Messages in INBOX: %d\n",CkImap_getNumMessages(imap));
    success = CkImap_Logout(imap);
    CkImap_Disconnect(imap);
    if (success != TRUE) {
        printf("The read-only operation completed, but IMAP logout failed.\n");
    }



    CkJsonObject_Dispose(token);
    CkImap_Dispose(imap);

    }