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

Gmail SMTP: Send with an App Password on Port 587

See more SMTP Examples

Send through Gmail on port 587 using required STARTTLS and an app password. This page replaces the former less-secure password-authentication instructions. Port 465 uses implicit TLS instead.

This sends a real email. Use a recipient you control and the account's authorized sender identity. Gmail sending limits still apply. For OAuth2, use the token-based sending example.

Use an account-specific app password generated after enabling 2-Step Verification, loaded from protected configuration. A normal Google password will not work. App passwords may be unavailable under account or Workspace policy; OAuth2 is preferred. Changing the Google account password revokes app passwords. The obsolete less-secure-apps switch is not a setup option. Google app-password requirements.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat SQL Server Downloads

SQL Server
-- 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.

    DECLARE @mailbox nvarchar(4000)
    SELECT @mailbox = 'user@example.com'
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.gmail.com'
    EXEC sp_OASetProperty @mailman, 'SmtpPort', 587
    EXEC sp_OASetProperty @mailman, 'SmtpSsl', 0
    EXEC sp_OASetProperty @mailman, 'StartTLS', 1
    EXEC sp_OASetProperty @mailman, 'RequireSslCertVerify', 1
    --  Alternative: port 465 with SmtpSsl=true and StartTLS=false (implicit TLS).
    EXEC sp_OASetProperty @mailman, 'SmtpUsername', @mailbox
    --  Load the app password from protected configuration.
    EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'YOUR_16_CHARACTER_APP_PASSWORD'

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'From', @mailbox
    EXEC sp_OASetProperty @email, 'Subject', 'Sample message'
    EXEC sp_OASetProperty @email, 'Body', 'This message was sent through Gmail SMTP using an app password.'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Recipient', 'recipient@example.net'
    IF @success <> 1
      BEGIN

        PRINT 'Could not add the recipient.'
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  This sends a real message. Replace the recipient with an address you control.
    EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
    IF @success <> 1
      BEGIN
        EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT

        PRINT 'SMTP send failed. Check the app password, mailbox, recipient, and Gmail sending limits.'
        --  Inspect LastErrorText privately if needed; do not publish authentication/session logs.
        --  A lost connection after DATA can leave acceptance uncertain. Avoid blindly retrying.
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Gmail accepted the message, but closing the connection failed. Do not resend it.'
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    PRINT 'Gmail accepted the message for delivery.'

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @email


END
GO