Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Variant vBdImage
    Handle hoBdImage
    String sCid
    Handle hoSbHtml
    Integer iNumReplaced
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Related image from BinData"

    //  Load the image data from a file into a BinData object.
    Get Create (RefClass(cComChilkatBinData)) To hoBdImage
    If (Not(IsComObjectCreated(hoBdImage))) Begin
        Send CreateComObject of hoBdImage
    End
    Get ComLoadFile Of hoBdImage "qa_data/images/logo.png" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoBdImage To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Add the image as a related item; capture its generated Content-ID.
    Get pvComObject of hoBdImage to vBdImage
    Get ComAddRelatedBd Of hoEmail "logo.png" vBdImage To sCid
    Get ComLastMethodSuccess Of hoEmail To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Reference the related item in the HTML body by its Content-ID.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHtml
    If (Not(IsComObjectCreated(hoSbHtml))) Begin
        Send CreateComObject of hoSbHtml
    End
    Get ComAppend Of hoSbHtml '<html><body><img src="cid:PLACEHOLDER_CID"/></body></html>' To iSuccess
    Get ComReplace Of hoSbHtml "PLACEHOLDER_CID" sCid To iNumReplaced
    Get ComGetAsString Of hoSbHtml To sTemp1
    Send ComSetHtmlBody To hoEmail sTemp1

    Get ComNumRelatedItems Of hoEmail To iTemp1
    Showln "NumRelatedItems = " iTemp1

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


End_Procedure