POP3 Implicit TLS: Gmail OAuth2 on Port 995
Authenticate to Gmail on port 995 with implicit TLS and inspect headers for up to ten currently available POP messages. UIDLs stay as strings. With headerOnly=true, a zero line-count argument retrieves headers plus the first body line. The example displays only headers and issues TOP, neither RETR nor DELE.
Enable POP in Gmail settings and review the option controlling Gmail's copy after access. Delivery windows, history, and batching mean available POP mail is not the entire Gmail mailbox. Use IMAP for folder/label access and synchronization.
First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.
Implicit TLS starts when the TCP connection opens. It is different from STLS on port 110. The historical password-based Gmail sample has been replaced with OAuth2.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-mail-token.json")
if not success then
print("Could not read gmail-mail-token.json from the private working directory.")
return
end
if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
print("An OAuth access token is required. Authorize or refresh the account first.")
return
end
local mailbox = "user@example.com"
local mailman = chilkat.MailMan.new()
mailman.mail_host = "pop.gmail.com"
mailman.mail_port = 995
mailman.pop_ssl = true
mailman.require_ssl_cert_verify = true
mailman.pop_username = mailbox
mailman.pop_password = ""
mailman.oauth2_access_token = token:string_of("access_token")
success = mailman:pop3_connect()
if not success then
print("Could not establish the Gmail POP3 TLS connection.")
return
end
success = mailman:pop3_authenticate()
if not success then
mailman:pop3_end_session_no_quit()
print("POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy.")
return
end
local uidls = chilkat.StringTable.new()
success = mailman:fetch_uidls(uidls)
if not success then
mailman:pop3_end_session_no_quit()
print("Could not list available POP UIDLs.")
return
end
local email = chilkat.Email.new()
local i = 0
while (i < uidls.count) and (i < 10) do
local uidl = uidls:string_at(i)
-- With headerOnly=true, zero requests headers plus the first body line.
-- Chilkat uses TOP rather than RETR; this example does not issue DELE.
success = mailman:fetch_by_uidl(uidl, true, 0, email)
if not success then
mailman:pop3_end_session_no_quit()
print("Could not read message headers.")
return
end
print("UIDL: " .. uidl)
print("From: " .. email.from)
print("Subject: " .. email.subject)
i = i + 1
end
success = mailman:pop3_end_session()
if not success then
print("Headers were read, but closing the POP session failed.")
return
end
print("Headers inspected: " .. i)