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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
string ls_Mailbox
oleobject loo_Imap
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Token.LoadFile("qa_data/tokens/office365.json")
if li_Success <> 1 then
Write-Debug "Could not read qa_data/tokens/office365.json from the private working directory."
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
Write-Debug "An OAuth access token is required. Authorize or refresh the account first."
destroy loo_Token
return
end if
ls_Mailbox = "user@example.com"
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("outlook.office365.com")
if li_Success <> 1 then
Write-Debug "Could not establish the Microsoft IMAP TLS connection."
destroy loo_Token
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login(ls_Mailbox,loo_Token.StringOf("access_token"))
if li_Success <> 1 then
loo_Imap.Disconnect()
Write-Debug "IMAP authentication failed. Check the mailbox, token scope, expiry, and account policy."
destroy loo_Token
destroy loo_Imap
return
end if
// EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
li_Success = loo_Imap.ExamineMailbox("INBOX")
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Could not examine INBOX."
destroy loo_Token
destroy loo_Imap
return
end if
Write-Debug "Messages in INBOX: " + string(loo_Imap.NumMessages)
li_Success = loo_Imap.Logout()
loo_Imap.Disconnect()
if li_Success <> 1 then
Write-Debug "The read-only operation completed, but IMAP logout failed."
end if
destroy loo_Token
destroy loo_Imap