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 Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Choose "imap", "smtp", or "pop". This example only tests authentication.
set protocol "imap"
set mailbox "user@example.com"
# Replace this placeholder with a value loaded from protected configuration.
set appPassword "YOUR_16_CHARACTER_APP_PASSWORD"
if {$protocol == "imap"} then {
set imap [new_CkImap]
CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
CkImap_put_RequireSslCertVerify $imap 1
set success [CkImap_Connect $imap "imap.gmail.com"]
if {$success != 1} then {
puts "Could not establish the Gmail IMAP TLS connection."
delete_CkImap $imap
exit
}
set success [CkImap_Login $imap $mailbox $appPassword]
if {$success != 1} then {
CkImap_Disconnect $imap
puts "IMAP authentication failed. Check the app password and account policy."
delete_CkImap $imap
exit
}
CkImap_Logout $imap
CkImap_Disconnect $imap
} else {
set mailman [new_CkMailMan]
CkMailMan_put_RequireSslCertVerify $mailman 1
if {$protocol == "smtp"} then {
CkMailMan_put_SmtpHost $mailman "smtp.gmail.com"
CkMailMan_put_SmtpPort $mailman 465
CkMailMan_put_SmtpSsl $mailman 1
CkMailMan_put_StartTLS $mailman 0
CkMailMan_put_SmtpUsername $mailman $mailbox
CkMailMan_put_SmtpPassword $mailman $appPassword
set success [CkMailMan_SmtpConnect $mailman]
if {$success != 1} then {
puts "Could not establish the Gmail SMTP TLS connection."
delete_CkImap $imap
delete_CkMailMan $mailman
exit
}
set success [CkMailMan_SmtpAuthenticate $mailman]
CkMailMan_CloseSmtpConnection $mailman
if {$success != 1} then {
puts "SMTP authentication failed. Check the app password and account policy."
delete_CkImap $imap
delete_CkMailMan $mailman
exit
}
} else {
if {$protocol != "pop"} then {
puts "Choose imap, smtp, or pop."
delete_CkImap $imap
delete_CkMailMan $mailman
exit
}
CkMailMan_put_MailHost $mailman "pop.gmail.com"
CkMailMan_put_MailPort $mailman 995
CkMailMan_put_PopSsl $mailman 1
CkMailMan_put_PopUsername $mailman $mailbox
CkMailMan_put_PopPassword $mailman $appPassword
set success [CkMailMan_Pop3Connect $mailman]
if {$success != 1} then {
puts "Could not establish the Gmail POP3 TLS connection."
delete_CkImap $imap
delete_CkMailMan $mailman
exit
}
set success [CkMailMan_Pop3Authenticate $mailman]
# Disconnect without QUIT; this authentication test makes no mailbox changes.
CkMailMan_Pop3EndSessionNoQuit $mailman
if {$success != 1} then {
puts "POP3 authentication failed. Check the app password, POP setting, and account policy."
delete_CkImap $imap
delete_CkMailMan $mailman
exit
}
}
}
puts "Gmail authentication succeeded using $protocol"
delete_CkImap $imap
delete_CkMailMan $mailman