Sample code for 30+ languages & platforms
Lazarus Pascal 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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Imap,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  token: TJsonObject;
  mailbox: string;
  imap: TImap;

begin
  success := False;

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

  token := TJsonObject.Create;
  success := token.LoadFile('qa_data/tokens/office365.json');
  if (success <> True) then
    begin
      WriteLn('Could not read qa_data/tokens/office365.json from the private working directory.');
      Exit;
    end;
  if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',True) = True)) then
    begin
      WriteLn('An OAuth access token is required. Authorize or refresh the account first.');
      Exit;
    end;
  mailbox := 'user@example.com';
  imap := TImap.Create;
  imap.Ssl := True;
  imap.Port := 993;
  imap.RequireSslCertVerify := True;
  imap.AuthMethod := 'XOAUTH2';
  success := imap.Connect('outlook.office365.com');
  if (success <> True) then
    begin
      WriteLn('Could not establish the Microsoft IMAP TLS connection.');
      Exit;
    end;
  success := imap.Login(mailbox,token.StringOf('access_token'));
  if (success <> True) then
    begin
      imap.Disconnect();
      WriteLn('IMAP authentication failed. Check the mailbox, token scope, expiry, and account policy.');
      Exit;
    end;
  //  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
  success := imap.ExamineMailbox('INBOX');
  if (success <> True) then
    begin
      imap.Logout();
      imap.Disconnect();
      WriteLn('Could not examine INBOX.');
      Exit;
    end;
  WriteLn('Messages in INBOX: ' + imap.NumMessages);
  success := imap.Logout();
  imap.Disconnect();
  if (success <> True) then
    begin
      WriteLn('The read-only operation completed, but IMAP logout failed.');
    end;


  token.Free;
  imap.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.