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 PowerBuilder Downloads
integer li_rc
long li_Success
string ls_Protocol
string ls_Mailbox
string ls_AppPassword
oleobject loo_Imap
oleobject loo_Mailman
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Choose "imap", "smtp", or "pop". This example only tests authentication.
ls_Protocol = "imap"
ls_Mailbox = "user@example.com"
// Replace this placeholder with a value loaded from protected configuration.
ls_AppPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
if ls_Protocol = "imap" then
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
destroy loo_Imap
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
Write-Debug "Could not establish the Gmail IMAP TLS connection."
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login(ls_Mailbox,ls_AppPassword)
if li_Success <> 1 then
loo_Imap.Disconnect()
Write-Debug "IMAP authentication failed. Check the app password and account policy."
destroy loo_Imap
return
end if
loo_Imap.Logout()
loo_Imap.Disconnect()
else
loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")
loo_Mailman.RequireSslCertVerify = 1
if ls_Protocol = "smtp" then
loo_Mailman.SmtpHost = "smtp.gmail.com"
loo_Mailman.SmtpPort = 465
loo_Mailman.SmtpSsl = 1
loo_Mailman.StartTLS = 0
loo_Mailman.SmtpUsername = ls_Mailbox
loo_Mailman.SmtpPassword = ls_AppPassword
li_Success = loo_Mailman.SmtpConnect()
if li_Success <> 1 then
Write-Debug "Could not establish the Gmail SMTP TLS connection."
destroy loo_Imap
destroy loo_Mailman
return
end if
li_Success = loo_Mailman.SmtpAuthenticate()
loo_Mailman.CloseSmtpConnection()
if li_Success <> 1 then
Write-Debug "SMTP authentication failed. Check the app password and account policy."
destroy loo_Imap
destroy loo_Mailman
return
end if
else
if ls_Protocol <> "pop" then
Write-Debug "Choose imap, smtp, or pop."
destroy loo_Imap
destroy loo_Mailman
return
end if
loo_Mailman.MailHost = "pop.gmail.com"
loo_Mailman.MailPort = 995
loo_Mailman.PopSsl = 1
loo_Mailman.PopUsername = ls_Mailbox
loo_Mailman.PopPassword = ls_AppPassword
li_Success = loo_Mailman.Pop3Connect()
if li_Success <> 1 then
Write-Debug "Could not establish the Gmail POP3 TLS connection."
destroy loo_Imap
destroy loo_Mailman
return
end if
li_Success = loo_Mailman.Pop3Authenticate()
// Disconnect without QUIT; this authentication test makes no mailbox changes.
loo_Mailman.Pop3EndSessionNoQuit()
if li_Success <> 1 then
Write-Debug "POP3 authentication failed. Check the app password, POP setting, and account policy."
destroy loo_Imap
destroy loo_Mailman
return
end if
end if
end if
Write-Debug "Gmail authentication succeeded using " + ls_Protocol
destroy loo_Imap
destroy loo_Mailman