Sample code for 30+ languages & platforms
Xojo Plugin

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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

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

Dim email As New Chilkat.Email
email.Subject = "Related image from BinData"

//  Load the image data from a file into a BinData object.
Dim bdImage As New Chilkat.BinData
success = bdImage.LoadFile("qa_data/images/logo.png")
If (success = False) Then
    System.DebugLog(bdImage.LastErrorText)
    Return
End If

//  Add the image as a related item; capture its generated Content-ID.
Dim cid As String
cid = email.AddRelatedBd("logo.png",bdImage)
If (email.LastMethodSuccess = False) Then
    System.DebugLog(email.LastErrorText)
    Return
End If

//  Reference the related item in the HTML body by its Content-ID.
Dim sbHtml As New Chilkat.StringBuilder
success = sbHtml.Append("<html><body><img src=""cid:PLACEHOLDER_CID""/></body></html>")
Dim numReplaced As Int32
numReplaced = sbHtml.Replace("PLACEHOLDER_CID",cid)
email.SetHtmlBody sbHtml.GetAsString()

System.DebugLog("NumRelatedItems = " + Str(email.NumRelatedItems))

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