Sample code for 30+ languages & platforms
SQL Server

SMTP Inspection

See more SMTP Examples

Examine an SMTP session to view all communications sent to the SMTP server.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    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.example.com'
    EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'myUsername'
    EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'myPassword'

    EXEC sp_OASetProperty @mailman, 'SmtpPort', 465
    EXEC sp_OASetProperty @mailman, 'StartTLS', 1

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

    EXEC sp_OASetProperty @email, 'Subject', 'This is a test'
    EXEC sp_OASetProperty @email, 'Body', 'This is a test'
    EXEC sp_OASetProperty @email, 'From', 'Alex <alex@example.com>'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Christoph', 'christoph@example2.com'
    EXEC sp_OAMethod @email, 'AddBcc', @success OUT, 'Victor', 'victor@example3.com'

    -- Generate the MIME that is sent when SendEmail is called
    DECLARE @mimeStr nvarchar(4000)
    EXEC sp_OAMethod @mailman, 'RenderToMime', @mimeStr OUT, @email

    PRINT @mimeStr

    EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Connection to SMTP server not closed cleanly.'
      END

    -- Examine the SMTP session log.

    PRINT '----'

    PRINT 'Smtp Session Log:'
    EXEC sp_OAGetProperty @mailman, 'SmtpSessionLog', @sTmp0 OUT
    PRINT @sTmp0

    -- ---------------------------------------------------
    -- Sample output:
    -- ---------------------------------------------------

    -- MIME-Version: 1.0
    -- Date: Fri, 06 Mar 2026 16:21:00 -0600
    -- Message-ID: <A5DC25990C93EC2676F4FBB2BB3BFA9EB34FF9D3@CHILKAT25>
    -- Content-Type: text/plain; charset=us-ascii; format=flowed
    -- Content-Transfer-Encoding: 7bit
    -- X-Priority: 3 (Normal)
    -- Subject: This is a test
    -- From: Alex <alex@example.com>
    -- To: Christoph <christoph@example2.com>
    -- Bcc: Victor <victor@example3.com>
    -- 
    -- This is a test
    -- ----
    -- Smtp Session Log:
    -- 220 smtp.example.com ESMTP Amazon WorkMail SMTP Service
    -- EHLO MYCOMPUTER<CRLF>
    -- 250-smtp.example.com
    -- 250-8BITMIME
    -- 250-AUTH PLAIN LOGIN
    -- 250 Ok
    -- AUTH LOGIN<CRLF>
    -- 334 VXNlcm5hbWU6
    -- YWRtaW5AY2hpbGthdHNvZnQuY29t<CRLF>
    -- 334 UGFzc3dvcmQ6
    -- {PasswordOrCredentials}
    -- 235 Authentication successful.
    -- MAIL FROM:<alex@example.com><CRLF>
    -- 250 Ok
    -- RCPT TO:<christoph@example2.com><CRLF>
    -- 250 Ok
    -- RCPT TO:<victor@example3.com><CRLF>
    -- 250 Ok
    -- DATA<CRLF>
    -- 354 End data with <CR><LF>.<CR><LF>
    -- {388 bytes} <--- The MIME source of the email (examined via RenderToMime) was sent here.
    -- <CRLF>.<CRLF>
    -- 250 Ok
    -- QUIT<CRLF>
    -- 221 Bye

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


END
GO