Sample code for 30+ languages & platforms
SQL Server

Get an Email's MIME into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetMimeBd method, which serializes the complete RFC822/MIME message (headers, body representations, related items, and attachments) and appends the bytes to a BinData object. This example builds a message, serializes it to a BinData, and prints the byte count.

Background: A BinData holds raw bytes, which is the right container when you want the MIME as binary rather than text — for example to write it directly to a file or socket, hash it, or hand it to another API that expects a byte buffer. It is the binary counterpart to GetMime (string) and GetMimeSb (StringBuilder).

Chilkat SQL Server Downloads

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

    --  Demonstrates the GetMimeBd method, which serializes the complete RFC822/MIME message
    --  (headers, bodies, related items, and attachments) and appends the bytes to a BinData
    --  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', 'GetMimeBd 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 BinData object.
    DECLARE @bdMime int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdMime OUT

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


    EXEC sp_OAGetProperty @bdMime, 'NumBytes', @iTmp0 OUT
    PRINT 'MIME size (bytes) = ' + @iTmp0

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @bdMime


END
GO