Sample code for 30+ languages & platforms
DataFlex

Add an Attachment from a BinData Object

See more Email Object Examples

Demonstrates the Chilkat Email.AddAttachmentBd method, which adds an attachment using the contents of a BinData object. The first argument is the attachment filename, the second is the BinData, and the third is the content type — if empty, it is inferred from the filename extension. This example loads a PDF into a BinData and attaches it.

Background: BinData is Chilkat's container for raw binary data. Attaching from a BinData is the right approach when the file's bytes are already in memory — generated on the fly, downloaded, or read from a database — rather than sitting on disk (which would use AddFileAttachment). Chilkat Base64-encodes the bytes into the message automatically.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Variant vBd
    Handle hoBd
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the AddAttachmentBd method, which adds an attachment using the contents of a
    //  BinData object.  The first argument is the attachment filename, the second is the BinData
    //  object, and the third is the content type (inferred from the filename extension if empty).

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Attach from BinData"
    Set ComBody Of hoEmail To "Please see the attached file."

    //  Load a file into a BinData object, then attach it.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComLoadFile Of hoBd "qa_data/attachments/report.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoBd To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoBd to vBd
    Get ComAddAttachmentBd Of hoEmail "report.pdf" vBd "application/pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumAttachments Of hoEmail To iTemp1
    Showln "NumAttachments = " iTemp1

    //  Note: The path "qa_data/attachments/report.pdf" is a relative local filesystem path,
    //  relative to the current working directory of the running application.


End_Procedure