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

POP3 GMail XOAUTH2 Authentication

See more GMail SMTP/IMAP/POP Examples

Demonstrates using OAuth2 authentication with pop.gmail.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 = "pop.gmail.com"
mailman.mail_port = 995
mailman.pop_ssl = true

mailman.pop_username = "my_account@gmail.com"

-- If using OAuth2 authentication, leave the password empty.
mailman.pop_password = ""

-- Load our previously obtained OAuth2 access token.
-- See Getting a GMail POP3 OAuth2 Access Token
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/gmail_pop3.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 POP3 server.
success = mailman:pop3_connect()
if not success then
    print(mailman.last_error_text)
    return
end

-- Authenticate..
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 pop.gmail.com:995
-- < +OK Gpop ready for requests from 87.9.200.42 l16mb86351205iok
-- > AUTH XOAUTH2 dXNlcj1....VaMDJFAQE=
-- < +OK Welcome.
-- > STAT
-- < +OK 301 1627357
--
-- -- Finished.

-- 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.")