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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Variant vData
    Handle hoData
    Variant vImage
    Handle hoImage
    String sCid
    Handle hoHtml
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get ComLoadEml Of hoEmail "text-and-html.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoData
    If (Not(IsComObjectCreated(hoData))) Begin
        Send CreateComObject of hoData
    End
    Get ComAppendString Of hoData "Time,Activity" + (character(13)) + (character(10)) + "09:00,Welcome" + (character(13)) + (character(10)) + "10:00,Field session" + (character(13)) + (character(10)) "utf-8" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed: data.AppendString."
        Procedure_Return
    End

    Get pvComObject of hoData to vData
    Get ComAddAttachmentBd Of hoEmail "agenda.csv" vData "text/csv" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
    Get Create (RefClass(cComChilkatBinData)) To hoImage
    If (Not(IsComObjectCreated(hoImage))) Begin
        Send CreateComObject of hoImage
    End
    Get ComAppendEncoded Of hoImage "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==" "base64" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed: image.AppendEncoded."
        Procedure_Return
    End

    Get pvComObject of hoImage to vImage
    Get ComAddRelatedBd Of hoEmail "confirmed.png" vImage To sCid
    Get ComLastMethodSuccess Of hoEmail To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoHtml
    If (Not(IsComObjectCreated(hoHtml))) Begin
        Send CreateComObject of hoHtml
    End
    Get ComAppend Of hoHtml '<p><img alt='Confirmed' src="cid:' To iSuccess
    Get ComAppend Of hoHtml sCid To iSuccess
    Get ComAppend Of hoHtml '"></p>' To iSuccess
    Get ComGetHtmlBody Of hoEmail To sTemp1
    Get ComAppend Of hoHtml sTemp1 To iSuccess
    Get ComGetAsString Of hoHtml To sTemp1
    Get ComAddHtmlAlternativeBody Of hoEmail sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSaveEml Of hoEmail "with-attachments.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Saved with-attachments.eml."


End_Procedure