Sample code for 30+ languages & platforms
SQL Server

Get an Email's MIME into a StringBuilder

See more Email Object Examples

Demonstrates the Chilkat Email.GetMimeSb method, which returns the email as MIME text (header, body or bodies, related items, and all attachments) by appending it to a StringBuilder object. This example builds a message and appends its MIME to a StringBuilder.

Background: This is a StringBuilder-based alternative to GetMime, which returns a plain string. Writing into a StringBuilder is more efficient when the MIME is large or when you intend to further manipulate it — searching, replacing, or accumulating additional text — without creating many 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 GetMimeSb method, which returns the email as MIME text (headers, bodies,
    --  related items, and attachments), appending it to a StringBuilder object.

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

    EXEC sp_OASetProperty @email, 'Subject', 'GetMimeSb example'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email, 'Body', 'Hello!'

    --  Append the complete MIME to a StringBuilder.
    DECLARE @sbMime int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT

    EXEC sp_OAMethod @email, 'GetMimeSb', @success OUT, @sbMime
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        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 @email
    EXEC @hr = sp_OADestroy @sbMime


END
GO