Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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).

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Attach from BinData")
    CkEmail::setCkBody(email, "Please see the attached file.")

    ;  Load a file into a BinData object, then attach it.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckLoadFile(bd,"qa_data/attachments/report.pdf")
    If success = 0
        Debug CkBinData::ckLastErrorText(bd)
        CkEmail::ckDispose(email)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    success = CkEmail::ckAddAttachmentBd(email,"report.pdf",bd,"application/pdf")
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "NumAttachments = " + Str(CkEmail::ckNumAttachments(email))

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


    CkEmail::ckDispose(email)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure