Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v9.5.0.83+

Office365 POP3 Login with OAuth2 Authentication

See more Office365 Examples

Demonstrates how to authenticate using OAuth2 using the POP3 protocol with outlook.office365.com.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mailman: HCkMailMan;
jsonToken: HCkJsonObject;
numEmails: Integer;

begin
success := False;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

mailman := CkMailMan_Create();

CkMailMan_putMailHost(mailman,'outlook.office365.com');
CkMailMan_putMailPort(mailman,995);
CkMailMan_putPopSsl(mailman,True);

//  Use your O365 email address here.
CkMailMan_putPopUsername(mailman,'OFFICE365_EMAIL_ADDRESS');

//  When using OAuth2 authentication, leave the password empty.
CkMailMan_putPopPassword(mailman,'');

//  Load our previously obtained OAuth2 access token.
jsonToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/office365.json');
if (success = False) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(jsonToken));
    Exit;
  end;

CkMailMan_putOAuth2AccessToken(mailman,CkJsonObject__stringOf(jsonToken,'access_token'));

//  Make the TLS connection to the outlook.office365.com POP3 server.
success := CkMailMan_Pop3Connect(mailman);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

//  Authenticate using XOAUTH2
success := CkMailMan_Pop3Authenticate(mailman);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

//  Find out how many emails are on the server..
numEmails := CkMailMan_CheckMail(mailman);
if (numEmails < 0) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

//  Examine the POP3 session log:
Memo1.Lines.Add(CkMailMan__pop3SessionLog(mailman));

//  The POP3 session log will look something like this:

//  **** Connected to outlook.office365.com:995
//  < +OK The Microsoft Exchange POP3 service is ready. [QwBIADIAUABSADEAOABD...YwBvAG0A]
//  > AUTH XOAUTH2
//  < + 
//  > <base64 string in XOAUTH2 format>
//  < +OK User successfully authenticated.
//  > STAT
//  < +OK 3 375302

//  End the POP3 session and close the connection to the GMail server.
success := CkMailMan_Pop3EndSession(mailman);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

Memo1.Lines.Add('Finished.');

CkMailMan_Dispose(mailman);
CkJsonObject_Dispose(jsonToken);