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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'qa_data/tokens/office365.json'
IF @success <> 1
BEGIN
PRINT 'Could not read qa_data/tokens/office365.json from the private working directory.'
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'An OAuth access token is required. Authorize or refresh the account first.'
EXEC @hr = sp_OADestroy @token
RETURN
END
DECLARE @mailbox nvarchar(4000)
SELECT @mailbox = 'user@example.com'
DECLARE @imap int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
EXEC sp_OASetProperty @imap, 'Ssl', 1
EXEC sp_OASetProperty @imap, 'Port', 993
EXEC sp_OASetProperty @imap, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @imap, 'AuthMethod', 'XOAUTH2'
EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'outlook.office365.com'
IF @success <> 1
BEGIN
PRINT 'Could not establish the Microsoft IMAP TLS connection.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @imap
RETURN
END
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @imap, 'Login', @success OUT, @mailbox, @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
PRINT 'IMAP authentication failed. Check the mailbox, token scope, expiry, and account policy.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
EXEC sp_OAMethod @imap, 'ExamineMailbox', @success OUT, 'INBOX'
IF @success <> 1
BEGIN
EXEC sp_OAMethod @imap, 'Logout', @success OUT
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
PRINT 'Could not examine INBOX.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @imap
RETURN
END
EXEC sp_OAGetProperty @imap, 'NumMessages', @iTmp0 OUT
PRINT 'Messages in INBOX: ' + @iTmp0
EXEC sp_OAMethod @imap, 'Logout', @success OUT
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
IF @success <> 1
BEGIN
PRINT 'The read-only operation completed, but IMAP logout failed.'
END
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @imap
END
GO