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

Lua
local chilkat = require("chilkat")

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

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 = ""

-- Load our previously obtained OAuth2 access token.
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/office365.json")
if not success then
    print(jsonToken.last_error_text)
    return
end

mailman.oauth2_access_token = jsonToken:string_of("access_token")

-- Make the TLS connection to the outlook.office365.com POP3 server.
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.")