Sample code for 30+ languages & platforms
SQL Server

Load an Email from MIME in a StringBuilder

See more Email Object Examples

Demonstrates the Chilkat Email.SetFromMimeSb method, which loads an email from the MIME text stored in a StringBuilder object. On success, it replaces the entire current email. This example serializes one email into a StringBuilder with GetMimeSb, then reconstructs it in a second object.

Background: This is the StringBuilder counterpart to SetFromMimeText. When the MIME already lives in a StringBuilder — perhaps assembled or edited there — passing it directly avoids converting to an intermediate string, which is more efficient for large messages.

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 SetFromMimeSb method, which loads an email from MIME text stored in a
    --  StringBuilder object.  On success, it replaces the entire current email.

    --  Build a source email and get its MIME into a StringBuilder.
    DECLARE @source int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @source OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @source, 'Subject', 'Source message'
    EXEC sp_OASetProperty @source, 'From', 'alice@example.com'
    EXEC sp_OASetProperty @source, 'Body', 'Hello from a StringBuilder.'
    DECLARE @sbMime int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT

    EXEC sp_OAMethod @source, 'GetMimeSb', @success OUT, @sbMime

    --  Load a new email object from the StringBuilder MIME.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

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


    EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT
    PRINT 'Loaded subject: ' + @sTmp0

    EXEC @hr = sp_OADestroy @source
    EXEC @hr = sp_OADestroy @sbMime
    EXEC @hr = sp_OADestroy @email


END
GO