Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_BdImage
string ls_Cid
oleobject loo_SbHtml
integer li_NumReplaced

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

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Email.Subject = "Related image from BinData"

//  Load the image data from a file into a BinData object.
loo_BdImage = create oleobject
li_rc = loo_BdImage.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_BdImage.LoadFile("qa_data/images/logo.png")
if li_Success = 0 then
    Write-Debug loo_BdImage.LastErrorText
    destroy loo_Email
    destroy loo_BdImage
    return
end if

//  Add the image as a related item; capture its generated Content-ID.
ls_Cid = loo_Email.AddRelatedBd("logo.png",loo_BdImage)
if loo_Email.LastMethodSuccess = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_BdImage
    return
end if

//  Reference the related item in the HTML body by its Content-ID.
loo_SbHtml = create oleobject
li_rc = loo_SbHtml.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbHtml.Append("<html><body><img src=~"cid:PLACEHOLDER_CID~"/></body></html>")
li_NumReplaced = loo_SbHtml.Replace("PLACEHOLDER_CID",ls_Cid)
loo_Email.SetHtmlBody(loo_SbHtml.GetAsString())

Write-Debug "NumRelatedItems = " + string(loo_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.


destroy loo_Email
destroy loo_BdImage
destroy loo_SbHtml