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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
; Choose "imap", "smtp", or "pop". This example only tests authentication.
Local $sProtocol = "imap"
Local $sMailbox = "user@example.com"
; Replace this placeholder with a value loaded from protected configuration.
Local $sAppPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
If ($sProtocol = "imap") Then
$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$bSuccess = $oImap.Connect("imap.gmail.com")
If ($bSuccess <> True) Then
ConsoleWrite("Could not establish the Gmail IMAP TLS connection." & @CRLF)
Exit
EndIf
$bSuccess = $oImap.Login($sMailbox,$sAppPassword)
If ($bSuccess <> True) Then
$oImap.Disconnect()
ConsoleWrite("IMAP authentication failed. Check the app password and account policy." & @CRLF)
Exit
EndIf
$oImap.Logout()
$oImap.Disconnect()
Else
$oMailman = ObjCreate("Chilkat.MailMan")
$oMailman.RequireSslCertVerify = True
If ($sProtocol = "smtp") Then
$oMailman.SmtpHost = "smtp.gmail.com"
$oMailman.SmtpPort = 465
$oMailman.SmtpSsl = True
$oMailman.StartTLS = False
$oMailman.SmtpUsername = $sMailbox
$oMailman.SmtpPassword = $sAppPassword
$bSuccess = $oMailman.SmtpConnect()
If ($bSuccess <> True) Then
ConsoleWrite("Could not establish the Gmail SMTP TLS connection." & @CRLF)
Exit
EndIf
$bSuccess = $oMailman.SmtpAuthenticate()
$oMailman.CloseSmtpConnection()
If ($bSuccess <> True) Then
ConsoleWrite("SMTP authentication failed. Check the app password and account policy." & @CRLF)
Exit
EndIf
Else
If ($sProtocol <> "pop") Then
ConsoleWrite("Choose imap, smtp, or pop." & @CRLF)
Exit
EndIf
$oMailman.MailHost = "pop.gmail.com"
$oMailman.MailPort = 995
$oMailman.PopSsl = True
$oMailman.PopUsername = $sMailbox
$oMailman.PopPassword = $sAppPassword
$bSuccess = $oMailman.Pop3Connect()
If ($bSuccess <> True) Then
ConsoleWrite("Could not establish the Gmail POP3 TLS connection." & @CRLF)
Exit
EndIf
$bSuccess = $oMailman.Pop3Authenticate()
; Disconnect without QUIT; this authentication test makes no mailbox changes.
$oMailman.Pop3EndSessionNoQuit()
If ($bSuccess <> True) Then
ConsoleWrite("POP3 authentication failed. Check the app password, POP setting, and account policy." & @CRLF)
Exit
EndIf
EndIf
EndIf
ConsoleWrite("Gmail authentication succeeded using " & $sProtocol & @CRLF)