Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcProtocol
LOCAL lcMailbox
LOCAL lcAppPassword
LOCAL loImap
LOCAL loMailman

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

*  Choose "imap", "smtp", or "pop". This example only tests authentication.
lcProtocol = "imap"
lcMailbox = "user@example.com"
*  Replace this placeholder with a value loaded from protected configuration.
lcAppPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
IF (lcProtocol = "imap") THEN
    loImap = CreateObject('Chilkat.Imap')
    loImap.Ssl = 1
    loImap.Port = 993
    loImap.RequireSslCertVerify = 1
    lnSuccess = loImap.Connect("imap.gmail.com")
    IF (lnSuccess <> 1) THEN
        ? "Could not establish the Gmail IMAP TLS connection."
        RELEASE loImap
        CANCEL
    ENDIF

    lnSuccess = loImap.Login(lcMailbox,lcAppPassword)
    IF (lnSuccess <> 1) THEN
        loImap.Disconnect()
        ? "IMAP authentication failed. Check the app password and account policy."
        RELEASE loImap
        CANCEL
    ENDIF

    loImap.Logout()
    loImap.Disconnect()
ELSE
    loMailman = CreateObject('Chilkat.MailMan')
    loMailman.RequireSslCertVerify = 1
    IF (lcProtocol = "smtp") THEN
        loMailman.SmtpHost = "smtp.gmail.com"
        loMailman.SmtpPort = 465
        loMailman.SmtpSsl = 1
        loMailman.StartTLS = 0
        loMailman.SmtpUsername = lcMailbox
        loMailman.SmtpPassword = lcAppPassword
        lnSuccess = loMailman.SmtpConnect()
        IF (lnSuccess <> 1) THEN
            ? "Could not establish the Gmail SMTP TLS connection."
            RELEASE loImap
            RELEASE loMailman
            CANCEL
        ENDIF

        lnSuccess = loMailman.SmtpAuthenticate()
        loMailman.CloseSmtpConnection()
        IF (lnSuccess <> 1) THEN
            ? "SMTP authentication failed. Check the app password and account policy."
            RELEASE loImap
            RELEASE loMailman
            CANCEL
        ENDIF

    ELSE
        IF (lcProtocol <> "pop") THEN
            ? "Choose imap, smtp, or pop."
            RELEASE loImap
            RELEASE loMailman
            CANCEL
        ENDIF

        loMailman.MailHost = "pop.gmail.com"
        loMailman.MailPort = 995
        loMailman.PopSsl = 1
        loMailman.PopUsername = lcMailbox
        loMailman.PopPassword = lcAppPassword
        lnSuccess = loMailman.Pop3Connect()
        IF (lnSuccess <> 1) THEN
            ? "Could not establish the Gmail POP3 TLS connection."
            RELEASE loImap
            RELEASE loMailman
            CANCEL
        ENDIF

        lnSuccess = loMailman.Pop3Authenticate()
        *  Disconnect without QUIT; this authentication test makes no mailbox changes.
        loMailman.Pop3EndSessionNoQuit()
        IF (lnSuccess <> 1) THEN
            ? "POP3 authentication failed. Check the app password, POP setting, and account policy."
            RELEASE loImap
            RELEASE loMailman
            CANCEL
        ENDIF

    ENDIF

ENDIF

? "Gmail authentication succeeded using " + lcProtocol

RELEASE loImap
RELEASE loMailman