Sample code for 30+ languages & platforms
SQL Server

Render an Email to MIME Bytes in a BinData

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMimeBd method, which renders an Email object as MIME bytes and appends the result to a BinData, without sending the email. This example renders a message into a BinData and prints the byte count.

Background: This is the binary version of RenderToMime. A BinData holds raw bytes, which is the right container when you want the rendered MIME as binary — to write it directly to a file or socket, hash it, or hand it to another API expecting a byte buffer — rather than as text.

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 MailMan.RenderToMimeBd method, which renders an Email object as MIME
    --  bytes and appends the result to a BinData (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 bytes in a BinData.'

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

    EXEC sp_OAMethod @mailman, 'RenderToMimeBd', @success OUT, @email, @bdMime
    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 @bdMime
        RETURN
      END


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

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


END
GO