Gmail: Authenticate with an App Password
See more GMail SMTP/IMAP/POP Examples
Authenticate to Gmail IMAP, SMTP, or POP3 with a Google app password. Choose one protocol in the code; the example authenticates and disconnects without sending, retrieving, or deleting messages.
OAuth2 is the recommended approach. An app password is an account-specific 16-character password generated under Google Account security settings after enabling 2-Step Verification. Use the generated value without display spaces, not your normal Google password. App passwords may be unavailable under Advanced Protection, security-key-only 2-Step Verification, or organization policy. Changing the Google account password revokes its app passwords.
Store the app password in protected configuration and load it at runtime. It is a credential, not an OAuth token, so this example uses the protocol password property and does not set OAuth2AccessToken. There is no supported instruction to enable “less secure apps.” POP must be enabled in Gmail settings; Workspace administrators can also restrict IMAP and POP. Personal Gmail IMAP is always enabled.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local success = false
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Choose "imap", "smtp", or "pop". This example only tests authentication.
local protocol = "imap"
local mailbox = "user@example.com"
-- Replace this placeholder with a value loaded from protected configuration.
local appPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
if protocol == "imap" then
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
imap.require_ssl_cert_verify = true
success = imap:connect("imap.gmail.com")
if not success then
print("Could not establish the Gmail IMAP TLS connection.")
return
end
success = imap:login(mailbox, appPassword)
if not success then
imap:disconnect()
print("IMAP authentication failed. Check the app password and account policy.")
return
end
imap:logout()
imap:disconnect()
else
local mailman = chilkat.MailMan.new()
mailman.require_ssl_cert_verify = true
if protocol == "smtp" then
mailman.smtp_host = "smtp.gmail.com"
mailman.smtp_port = 465
mailman.smtp_ssl = true
mailman.start_tls = false
mailman.smtp_username = mailbox
mailman.smtp_password = appPassword
success = mailman:smtp_connect()
if not success then
print("Could not establish the Gmail SMTP TLS connection.")
return
end
success = mailman:smtp_authenticate()
mailman:close_smtp_connection()
if not success then
print("SMTP authentication failed. Check the app password and account policy.")
return
end
else
if protocol ~= "pop" then
print("Choose imap, smtp, or pop.")
return
end
mailman.mail_host = "pop.gmail.com"
mailman.mail_port = 995
mailman.pop_ssl = true
mailman.pop_username = mailbox
mailman.pop_password = appPassword
success = mailman:pop3_connect()
if not success then
print("Could not establish the Gmail POP3 TLS connection.")
return
end
success = mailman:pop3_authenticate()
-- Disconnect without QUIT; this authentication test makes no mailbox changes.
mailman:pop3_end_session_no_quit()
if not success then
print("POP3 authentication failed. Check the app password, POP setting, and account policy.")
return
end
end
end
print("Gmail authentication succeeded using " .. protocol)