Sample code for 30+ languages & platforms
SQL Server

Render an Email to MIME in a StringBuilder

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMimeSb method, which renders an Email object as MIME text and appends the result to a StringBuilder, without sending the email. This example renders a message into a StringBuilder and prints it.

Background: This is the StringBuilder version of RenderToMime. Appending into a StringBuilder is more efficient when the MIME is large or when you plan to further inspect or manipulate it — searching, replacing, or accumulating additional text — without creating extra intermediate string copies.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the MailMan.RenderToMimeSb method, which renders an Email object as MIME text
    --  and appends the result to a StringBuilder (without 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'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email, 'Body', 'This message is rendered to MIME in a StringBuilder.'

    --  Render the MIME into a StringBuilder.
    DECLARE @sbMime int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT

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

    EXEC sp_OAMethod @sbMime, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

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


END
GO