Sample code for 30+ languages & platforms
VBScript Requires Chilkat v11.0.0+

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.

Google documentation

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

'  Choose "imap", "smtp", or "pop". This example only tests authentication.
protocol = "imap"
mailbox = "user@example.com"
'  Replace this placeholder with a value loaded from protected configuration.
appPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
If (protocol = "imap") Then
    set imap = CreateObject("Chilkat.Imap")
    imap.Ssl = 1
    imap.Port = 993
    imap.RequireSslCertVerify = 1
    success = imap.Connect("imap.gmail.com")
    If (success <> 1) Then
        outFile.WriteLine("Could not establish the Gmail IMAP TLS connection.")
        WScript.Quit
    End If

    success = imap.Login(mailbox,appPassword)
    If (success <> 1) Then
        success = imap.Disconnect()
        outFile.WriteLine("IMAP authentication failed. Check the app password and account policy.")
        WScript.Quit
    End If

    success = imap.Logout()
    success = imap.Disconnect()
Else
    set mailman = CreateObject("Chilkat.MailMan")
    mailman.RequireSslCertVerify = 1
    If (protocol = "smtp") Then
        mailman.SmtpHost = "smtp.gmail.com"
        mailman.SmtpPort = 465
        mailman.SmtpSsl = 1
        mailman.StartTLS = 0
        mailman.SmtpUsername = mailbox
        mailman.SmtpPassword = appPassword
        success = mailman.SmtpConnect()
        If (success <> 1) Then
            outFile.WriteLine("Could not establish the Gmail SMTP TLS connection.")
            WScript.Quit
        End If

        success = mailman.SmtpAuthenticate()
        success = mailman.CloseSmtpConnection()
        If (success <> 1) Then
            outFile.WriteLine("SMTP authentication failed. Check the app password and account policy.")
            WScript.Quit
        End If

    Else
        If (protocol <> "pop") Then
            outFile.WriteLine("Choose imap, smtp, or pop.")
            WScript.Quit
        End If

        mailman.MailHost = "pop.gmail.com"
        mailman.MailPort = 995
        mailman.PopSsl = 1
        mailman.PopUsername = mailbox
        mailman.PopPassword = appPassword
        success = mailman.Pop3Connect()
        If (success <> 1) Then
            outFile.WriteLine("Could not establish the Gmail POP3 TLS connection.")
            WScript.Quit
        End If

        success = mailman.Pop3Authenticate()
        '  Disconnect without QUIT; this authentication test makes no mailbox changes.
        success = mailman.Pop3EndSessionNoQuit()
        If (success <> 1) Then
            outFile.WriteLine("POP3 authentication failed. Check the app password, POP setting, and account policy.")
            WScript.Quit
        End If

    End If

End If

outFile.WriteLine("Gmail authentication succeeded using " & protocol)

outFile.Close