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

Ruby
require 'chilkat'

success = false

#  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")
    imap = Chilkat::CkImap.new()
    imap.put_Ssl(true)
    imap.put_Port(993)
    imap.put_RequireSslCertVerify(true)
    success = imap.Connect("imap.gmail.com")
    if (success != true)
        print "Could not establish the Gmail IMAP TLS connection." + "\n";
        exit
    end

    success = imap.Login(mailbox,appPassword)
    if (success != true)
        imap.Disconnect()
        print "IMAP authentication failed. Check the app password and account policy." + "\n";
        exit
    end

    imap.Logout()
    imap.Disconnect()
else
    mailman = Chilkat::CkMailMan.new()
    mailman.put_RequireSslCertVerify(true)
    if (protocol == "smtp")
        mailman.put_SmtpHost("smtp.gmail.com")
        mailman.put_SmtpPort(465)
        mailman.put_SmtpSsl(true)
        mailman.put_StartTLS(false)
        mailman.put_SmtpUsername(mailbox)
        mailman.put_SmtpPassword(appPassword)
        success = mailman.SmtpConnect()
        if (success != true)
            print "Could not establish the Gmail SMTP TLS connection." + "\n";
            exit
        end

        success = mailman.SmtpAuthenticate()
        mailman.CloseSmtpConnection()
        if (success != true)
            print "SMTP authentication failed. Check the app password and account policy." + "\n";
            exit
        end

    else
        if (protocol != "pop")
            print "Choose imap, smtp, or pop." + "\n";
            exit
        end

        mailman.put_MailHost("pop.gmail.com")
        mailman.put_MailPort(995)
        mailman.put_PopSsl(true)
        mailman.put_PopUsername(mailbox)
        mailman.put_PopPassword(appPassword)
        success = mailman.Pop3Connect()
        if (success != true)
            print "Could not establish the Gmail POP3 TLS connection." + "\n";
            exit
        end

        success = mailman.Pop3Authenticate()
        #  Disconnect without QUIT; this authentication test makes no mailbox changes.
        mailman.Pop3EndSessionNoQuit()
        if (success != true)
            print "POP3 authentication failed. Check the app password, POP setting, and account policy." + "\n";
            exit
        end

    end

end

print "Gmail authentication succeeded using " + protocol + "\n";