Sample code for 30+ languages & platforms
SQL Server

Render an Email to MIME Without Sending

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMime method, which renders an Email object as the MIME text that would be sent to the SMTP server, without actually sending it. This example builds a message and prints the rendered MIME.

Background: Rendering shows you exactly what MailMan would put on the wire — including headers it adds at send time and any signing/encryption — which is invaluable for debugging or logging before committing to a send. Notably, the Date header is filled in with the current date/time and a unique Message-ID is generated, while headers such as Content-Type, Content-Transfer-Encoding, X-Priority, and MIME-Version match what Email.GetMime would produce. Nothing is transmitted, so no SMTP connection is made.

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
    --  Demonstrates the MailMan.RenderToMime method, which renders an Email object as the MIME
    --  text that would be sent to the SMTP server, without actually sending the email.

    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Build the email to render.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'Rendered email'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'
    DECLARE @success int
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email, 'Body', 'This message is rendered to MIME, not sent.'

    --  Render to MIME (no connection to the SMTP server is made).
    DECLARE @mime nvarchar(4000)
    EXEC sp_OAMethod @mailman, 'RenderToMime', @mime OUT, @email

    PRINT @mime

    --  Sample output:
    --  
    --    MIME-Version: 1.0
    --    Date: Fri, 17 Jul 2026 05:25:42 -0500
    --    Message-ID: <E13F164829E44779462366BF1D8020CA210F8F1F@CHILKAT25>
    --    Content-Type: text/plain; charset=us-ascii; format=flowed
    --    Content-Transfer-Encoding: 7bit
    --    X-Priority: 3 (Normal)
    --    Subject: Rendered email
    --    From: alice@example.com
    --    To: Bob <bob@example.com>
    --  
    --    This message is rendered to MIME, not sent.
    --  
    --  Note: The Date header is automatically added with the current date/time, and a unique
    --  Message-ID header is generated and added.  The other headers (Content-Type,
    --  Content-Transfer-Encoding, X-Priority, and MIME-Version) have the same values you would
    --  see with Email.GetMime.

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


END
GO