Sample code for 30+ languages & platforms
SQL Server

Add a Related Item from BinData by Content-Location

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedBd2 method, which adds a related item using the binary data in a BinData object, addressed by Content-Location. The second argument specifies the filename, path, or URL used by the corresponding HTML reference. This example embeds an image referenced as logo.png.

Background: This is the binary counterpart of AddRelatedString2 and the Content-Location sibling of AddRelatedBd. Instead of a generated cid: reference, the HTML keeps an ordinary src="logo.png" and the related part is matched to it by name — convenient when turning an existing web page into an email so its original relative URLs keep working.

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 AddRelatedBd2 method, which adds a related item using the binary data in
    --  a BinData object, addressed by Content-Location.  The second argument specifies the filename/path/URL
    --  used by the corresponding HTML reference.

    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 (Content-Location)'

    --  The HTML references the image by the same name used as the Content-Location.
    EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, '<html><body><img src="logo.png"/></body></html>'

    --  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 addressed by Content-Location "logo.png".
    EXEC sp_OAMethod @email, 'AddRelatedBd2', @success OUT, @bdImage, 'logo.png'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @bdImage
        RETURN
      END


    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


END
GO