Sample code for 30+ languages & platforms
SQL Server

Get an Alternative Body into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyBd method, which copies the contents of the Nth alternative body into a BinData object. The first alternative body is at index 0, and it should only be called when NumAlternatives is greater than 0. This example builds a message with plain-text and HTML alternatives and copies the first into a BinData.

Background: This is the binary counterpart to GetAlternativeBody (which returns text). Reading an alternative into a BinData preserves the exact bytes — important when a body part uses a binary or unusual transfer encoding, or when you intend to hash it or write it out verbatim rather than decode it to a string.

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 GetAlternativeBodyBd method, which copies the contents of the Nth
    --  alternative body into a BinData object.  The first alternative body is at index 0; call
    --  only when NumAlternatives is greater than 0.

    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', 'GetAlternativeBodyBd example'

    EXEC sp_OAMethod @email, 'SetTextBody', NULL, 'This is the plain-text alternative.', 'text/plain'
    EXEC sp_OAMethod @email, 'AddHtmlAlternativeBody', @success OUT, '<html><body>This is the HTML alternative.</body></html>'

    --  Copy the first alternative body (index 0) into a BinData object.
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

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


    EXEC sp_OAGetProperty @bd, 'NumBytes', @iTmp0 OUT
    PRINT 'Alternative 0 size (bytes) = ' + @iTmp0

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @bd


END
GO