Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oEmail
LOCAL oData
LOCAL oImage
LOCAL cCid
LOCAL oHtml

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

oEmail := CreateObject("Chilkat.Email")
nSuccess := oEmail:LoadEml("text-and-html.eml")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    RETURN
ENDIF

oData := CreateObject("Chilkat.BinData")
nSuccess := oData:AppendString("Time,Activity" + Chr(13) + Chr(10) + "09:00,Welcome" + Chr(13) + Chr(10) + "10:00,Field session" + Chr(13) + Chr(10), "utf-8")
IF (nSuccess != 1)
    ? "Failed: data.AppendString."
    oEmail:destroy()
    oData:destroy()
    RETURN
ENDIF

nSuccess := oEmail:AddAttachmentBd("agenda.csv", oData, "text/csv")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oData:destroy()
    RETURN
ENDIF

//  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
oImage := CreateObject("Chilkat.BinData")
nSuccess := oImage:AppendEncoded("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==", "base64")
IF (nSuccess != 1)
    ? "Failed: image.AppendEncoded."
    oEmail:destroy()
    oData:destroy()
    oImage:destroy()
    RETURN
ENDIF

cCid := oEmail:AddRelatedBd("confirmed.png", oImage)
IF (oEmail:LastMethodSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oData:destroy()
    oImage:destroy()
    RETURN
ENDIF

oHtml := CreateObject("Chilkat.StringBuilder")
oHtml:Append('<p><img alt='Confirmed' src="cid:')
oHtml:Append(cCid)
oHtml:Append('"></p>')
oHtml:Append(oEmail:GetHtmlBody())
nSuccess := oEmail:AddHtmlAlternativeBody(oHtml:GetAsString())
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oData:destroy()
    oImage:destroy()
    oHtml:destroy()
    RETURN
ENDIF

nSuccess := oEmail:SaveEml("with-attachments.eml")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oData:destroy()
    oImage:destroy()
    oHtml:destroy()
    RETURN
ENDIF

? "Saved with-attachments.eml."

oEmail:destroy()
oData:destroy()
oImage:destroy()
oHtml:destroy()