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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Choose "imap", "smtp", or "pop". This example only tests authentication.
DECLARE @protocol nvarchar(4000)
SELECT @protocol = 'imap'
DECLARE @mailbox nvarchar(4000)
SELECT @mailbox = 'user@example.com'
-- Replace this placeholder with a value loaded from protected configuration.
DECLARE @appPassword nvarchar(4000)
SELECT @appPassword = 'YOUR_16_CHARACTER_APP_PASSWORD'
IF @protocol = 'imap'
BEGIN
DECLARE @imap int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @imap, 'Ssl', 1
EXEC sp_OASetProperty @imap, 'Port', 993
EXEC sp_OASetProperty @imap, 'RequireSslCertVerify', 1
EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.gmail.com'
IF @success <> 1
BEGIN
PRINT 'Could not establish the Gmail IMAP TLS connection.'
EXEC @hr = sp_OADestroy @imap
RETURN
END
EXEC sp_OAMethod @imap, 'Login', @success OUT, @mailbox, @appPassword
IF @success <> 1
BEGIN
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
PRINT 'IMAP authentication failed. Check the app password and account policy.'
EXEC @hr = sp_OADestroy @imap
RETURN
END
EXEC sp_OAMethod @imap, 'Logout', @success OUT
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
END
ELSE
BEGIN
DECLARE @mailman int
EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
EXEC sp_OASetProperty @mailman, 'RequireSslCertVerify', 1
IF @protocol = 'smtp'
BEGIN
EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.gmail.com'
EXEC sp_OASetProperty @mailman, 'SmtpPort', 465
EXEC sp_OASetProperty @mailman, 'SmtpSsl', 1
EXEC sp_OASetProperty @mailman, 'StartTLS', 0
EXEC sp_OASetProperty @mailman, 'SmtpUsername', @mailbox
EXEC sp_OASetProperty @mailman, 'SmtpPassword', @appPassword
EXEC sp_OAMethod @mailman, 'SmtpConnect', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not establish the Gmail SMTP TLS connection.'
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
RETURN
END
EXEC sp_OAMethod @mailman, 'SmtpAuthenticate', @success OUT
EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT
IF @success <> 1
BEGIN
PRINT 'SMTP authentication failed. Check the app password and account policy.'
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
RETURN
END
END
ELSE
BEGIN
IF @protocol <> 'pop'
BEGIN
PRINT 'Choose imap, smtp, or pop.'
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
RETURN
END
EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.gmail.com'
EXEC sp_OASetProperty @mailman, 'MailPort', 995
EXEC sp_OASetProperty @mailman, 'PopSsl', 1
EXEC sp_OASetProperty @mailman, 'PopUsername', @mailbox
EXEC sp_OASetProperty @mailman, 'PopPassword', @appPassword
EXEC sp_OAMethod @mailman, 'Pop3Connect', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not establish the Gmail POP3 TLS connection.'
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
RETURN
END
EXEC sp_OAMethod @mailman, 'Pop3Authenticate', @success OUT
-- Disconnect without QUIT; this authentication test makes no mailbox changes.
EXEC sp_OAMethod @mailman, 'Pop3EndSessionNoQuit', @success OUT
IF @success <> 1
BEGIN
PRINT 'POP3 authentication failed. Check the app password, POP setting, and account policy.'
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
RETURN
END
END
END
PRINT 'Gmail authentication succeeded using ' + @protocol
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
END
GO