Sample code for 30+ languages & platforms
B4X 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 B4X Downloads

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim email As ChilkatEmail
email.Initialize
success = email.LoadEml("text-and-html.eml")
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

Dim data As ChilkatBinData
data.Initialize
success = data.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 success <> True Then
    Log("Failed: data.AppendString.")
    Return
End If

success = email.AddAttachmentBd("agenda.csv", data, "text/csv")
If success <> True Then
    Log(email.LastErrorText)
    Return
End If


'  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
Dim image As ChilkatBinData
image.Initialize
success = image.AppendEncoded("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==", "base64")
If success <> True Then
    Log("Failed: image.AppendEncoded.")
    Return
End If

Dim cid As String = email.AddRelatedBd("confirmed.png", image)
If email.LastMethodSuccess <> True Then
    Log(email.LastErrorText)
    Return
End If

Dim html As ChilkatStringBuilder
html.Initialize
html.Append("<p><img alt='Confirmed' src=" & Chr(34) & "cid:")
html.Append(cid)
html.Append(Chr(34) & "></p>")
html.Append(email.GetHtmlBody)
success = email.AddHtmlAlternativeBody(html.GetAsString)
If success <> True Then
    Log(email.LastErrorText)
    Return
End If


success = email.SaveEml("with-attachments.eml")
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

Log("Saved with-attachments.eml.")