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 PureBasic Downloads
IncludeFile "CkImap.pb"
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
; Choose "imap", "smtp", or "pop". This example only tests authentication.
protocol.s = "imap"
mailbox.s = "user@example.com"
; Replace this placeholder with a value loaded from protected configuration.
appPassword.s = "YOUR_16_CHARACTER_APP_PASSWORD"
If protocol = "imap"
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
CkImap::setCkRequireSslCertVerify(imap, 1)
success = CkImap::ckConnect(imap,"imap.gmail.com")
If success <> 1
Debug "Could not establish the Gmail IMAP TLS connection."
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,mailbox,appPassword)
If success <> 1
CkImap::ckDisconnect(imap)
Debug "IMAP authentication failed. Check the app password and account policy."
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Else
mailman.i = CkMailMan::ckCreate()
If mailman.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkMailMan::setCkRequireSslCertVerify(mailman, 1)
If protocol = "smtp"
CkMailMan::setCkSmtpHost(mailman, "smtp.gmail.com")
CkMailMan::setCkSmtpPort(mailman, 465)
CkMailMan::setCkSmtpSsl(mailman, 1)
CkMailMan::setCkStartTLS(mailman, 0)
CkMailMan::setCkSmtpUsername(mailman, mailbox)
CkMailMan::setCkSmtpPassword(mailman, appPassword)
success = CkMailMan::ckSmtpConnect(mailman)
If success <> 1
Debug "Could not establish the Gmail SMTP TLS connection."
CkImap::ckDispose(imap)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
success = CkMailMan::ckSmtpAuthenticate(mailman)
CkMailMan::ckCloseSmtpConnection(mailman)
If success <> 1
Debug "SMTP authentication failed. Check the app password and account policy."
CkImap::ckDispose(imap)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
Else
If protocol <> "pop"
Debug "Choose imap, smtp, or pop."
CkImap::ckDispose(imap)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
CkMailMan::setCkMailHost(mailman, "pop.gmail.com")
CkMailMan::setCkMailPort(mailman, 995)
CkMailMan::setCkPopSsl(mailman, 1)
CkMailMan::setCkPopUsername(mailman, mailbox)
CkMailMan::setCkPopPassword(mailman, appPassword)
success = CkMailMan::ckPop3Connect(mailman)
If success <> 1
Debug "Could not establish the Gmail POP3 TLS connection."
CkImap::ckDispose(imap)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
success = CkMailMan::ckPop3Authenticate(mailman)
; Disconnect without QUIT; this authentication test makes no mailbox changes.
CkMailMan::ckPop3EndSessionNoQuit(mailman)
If success <> 1
Debug "POP3 authentication failed. Check the app password, POP setting, and account policy."
CkImap::ckDispose(imap)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
EndIf
EndIf
Debug "Gmail authentication succeeded using " + protocol
CkImap::ckDispose(imap)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndProcedure