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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sProtocol
String sMailbox
String sAppPassword
Handle hoImap
Handle hoMailman
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Choose "imap", "smtp", or "pop". This example only tests authentication.
Move "imap" To sProtocol
Move "user@example.com" To sMailbox
// Replace this placeholder with a value loaded from protected configuration.
Move "YOUR_16_CHARACTER_APP_PASSWORD" To sAppPassword
If (sProtocol = "imap") Begin
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Set ComRequireSslCertVerify Of hoImap To True
Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail IMAP TLS connection."
Procedure_Return
End
Get ComLogin Of hoImap sMailbox sAppPassword To iSuccess
If (iSuccess <> True) Begin
Get ComDisconnect Of hoImap To iSuccess
Showln "IMAP authentication failed. Check the app password and account policy."
Procedure_Return
End
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
End
Else Begin
Get Create (RefClass(cComChilkatMailMan)) To hoMailman
If (Not(IsComObjectCreated(hoMailman))) Begin
Send CreateComObject of hoMailman
End
Set ComRequireSslCertVerify Of hoMailman To True
If (sProtocol = "smtp") Begin
Set ComSmtpHost Of hoMailman To "smtp.gmail.com"
Set ComSmtpPort Of hoMailman To 465
Set ComSmtpSsl Of hoMailman To True
Set ComStartTLS Of hoMailman To False
Set ComSmtpUsername Of hoMailman To sMailbox
Set ComSmtpPassword Of hoMailman To sAppPassword
Get ComSmtpConnect Of hoMailman To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail SMTP TLS connection."
Procedure_Return
End
Get ComSmtpAuthenticate Of hoMailman To iSuccess
Get ComCloseSmtpConnection Of hoMailman To iSuccess
If (iSuccess <> True) Begin
Showln "SMTP authentication failed. Check the app password and account policy."
Procedure_Return
End
End
Else Begin
If (sProtocol <> "pop") Begin
Showln "Choose imap, smtp, or pop."
Procedure_Return
End
Set ComMailHost Of hoMailman To "pop.gmail.com"
Set ComMailPort Of hoMailman To 995
Set ComPopSsl Of hoMailman To True
Set ComPopUsername Of hoMailman To sMailbox
Set ComPopPassword Of hoMailman To sAppPassword
Get ComPop3Connect Of hoMailman To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail POP3 TLS connection."
Procedure_Return
End
Get ComPop3Authenticate Of hoMailman To iSuccess
// Disconnect without QUIT; this authentication test makes no mailbox changes.
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
If (iSuccess <> True) Begin
Showln "POP3 authentication failed. Check the app password, POP setting, and account policy."
Procedure_Return
End
End
End
Showln "Gmail authentication succeeded using " sProtocol
End_Procedure