Lua Requires Chilkat v9.5.0.97+
Lua
Office365 POP3 with Client Credentials
See more Office365 Examples
Demonstrates how to interact with the Office365 POP3 server using OAuth2 client credentials. This example provides the client ID, client secret, and tenant ID to Chilkat, and internally Chilkat will fetch the OAuth2 access token via client credentials as necessary. Therefore, it is not necessary for the application to explicitly fetch the OAuth2 access token beforehand.Note: This requires the Azure Entra ID application to be configured for client credentials as detailed in this guide: Office365 App Setup for SMTP, POP, IMAP OAuth2 Client Credentials
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- ----------------------------------------------------------------------------------------
-- Important:
-- Your Azure Entra ID app must be explicitly setup to allow for the OAuth2 client credentials flow.
-- Please follow the detailed guide for how to do it here:
-- Office365 App Setup for SMTP, POP, IMAP OAuth2 Client Credentials
--
-- Note: Your registered App must have the SMTP.SendAsApp permission.
-- ----------------------------------------------------------------------------------------
local json = chilkat.JsonObject.new()
-- Use your App's actual client ID here:
json:update_string("client_id", "2871da2c-8176-4b7f-869b-2311aa82e743")
-- Use your App's actual client secret here:
json:update_string("client_secret", "2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH")
-- Use this specific scope.
json:update_string("scope", "https://outlook.office365.com/.default")
-- Replace the tenant ID part of the URL with your tenant ID.
json:update_string("token_endpoint", "https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token")
local mailman = chilkat.MailMan.new()
mailman.mail_host = "outlook.office365.com"
mailman.mail_port = 995
mailman.pop_ssl = true
-- Use your O365 email address here.
mailman.pop_username = "OFFICE365_EMAIL_ADDRESS"
-- When using OAuth2 authentication, leave the password empty.
mailman.pop_password = ""
-- Instead of providing a previously fetched OAuth2 access token, we instead provide the information (above)
-- required for Chilkat to automatically fetch the access token via client credentials.
mailman.oauth2_access_token = json:emit()
-- Make the TLS connection to the outlook.office365.com POP3 server.
local success = mailman:pop3_connect()
if not success then
print(mailman.last_error_text)
return
end
-- Authenticate using XOAUTH2
success = mailman:pop3_authenticate()
if not success then
print(mailman.last_error_text)
return
end
-- Find out how many emails are on the server..
local numEmails = mailman:check_mail()
if numEmails < 0 then
print(mailman.last_error_text)
return
end
-- Examine the POP3 session log:
print(mailman.pop3_session_log)
-- 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 = mailman:pop3_end_session()
if not success then
print(mailman.last_error_text)
return
end
print("Finished.")