Sample code for 30+ languages & platforms
SQL Server

Add a Related Item from a BinData Object

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedBd method, which adds a related item (such as an inline image) using the contents of a BinData object, and returns the generated Content-ID. This example loads an image into a BinData, adds it, captures the Content-ID, and references it from the HTML body.

Background: This is the binary, Content-ID-based way to embed an inline resource — the right choice for images, whose bytes belong in a BinData rather than a string. Because Chilkat generates the Content-ID, the usual pattern is: add the item first, capture the returned ID, then build the matching <img src="cid:..."> reference from it (done here with a StringBuilder).

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the AddRelatedBd method, which adds a related item (such as an inline image)
    --  using the contents of a BinData object, and returns the generated Content-ID.

    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', 'Related image from BinData'

    --  Load the image data from a file into a BinData object.
    DECLARE @bdImage int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdImage OUT

    EXEC sp_OAMethod @bdImage, 'LoadFile', @success OUT, 'qa_data/images/logo.png'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bdImage, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @bdImage
        RETURN
      END

    --  Add the image as a related item; capture its generated Content-ID.
    DECLARE @cid nvarchar(4000)
    EXEC sp_OAMethod @email, 'AddRelatedBd', @cid OUT, 'logo.png', @bdImage
    EXEC sp_OAGetProperty @email, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @bdImage
        RETURN
      END

    --  Reference the related item in the HTML body by its Content-ID.
    DECLARE @sbHtml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbHtml OUT

    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, '<html><body><img src="cid:PLACEHOLDER_CID"/></body></html>'
    DECLARE @numReplaced int
    EXEC sp_OAMethod @sbHtml, 'Replace', @numReplaced OUT, 'PLACEHOLDER_CID', @cid
    EXEC sp_OAMethod @sbHtml, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, @sTmp0


    EXEC sp_OAGetProperty @email, 'NumRelatedItems', @iTmp0 OUT
    PRINT 'NumRelatedItems = ' + @iTmp0

    --  Note: The path "qa_data/images/logo.png" is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @bdImage
    EXEC @hr = sp_OADestroy @sbHtml


END
GO