Sample code for 30+ languages & platforms
PureBasic

Get the Nth Binary Part of a Content-Type into BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth MIME sub-part matching a content-type pattern into a BinData object. The arguments are the zero-based index among the matching parts, the content-type pattern, an inlineOnly flag, an excludeAttachments flag, and the BinData that receives the bytes. This example extracts the first image/png part.

Background: This is the binary, type-targeted way to pull a specific part out of a message — ideal for extracting, say, every image/png or the one application/pdf from a complex MIME tree without caring whether it is an attachment, an inline image, or a body part. Reading into a BinData keeps the raw bytes exact, ready to save, hash, or re-transmit.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth
    ;  MIME sub-part matching a Content-Type pattern into a BinData object.  The arguments are
    ;  the zero-based index among matching parts, the Content-Type pattern, inlineOnly,
    ;  excludeAttachments, and the BinData that receives the bytes.

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

    CkEmail::setCkSubject(email, "GetNthBinaryPartOfTypeBd example")
    CkEmail::setCkBody(email, "See the attached image.")

    ;  Load the image from a file into a BinData object and attach it (binary data belongs
    ;  in a BinData, never in a string).
    bdImage.i = CkBinData::ckCreate()
    If bdImage.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckLoadFile(bdImage,"qa_data/images/photo.png")
    If success = 0
        Debug CkBinData::ckLastErrorText(bdImage)
        CkEmail::ckDispose(email)
        CkBinData::ckDispose(bdImage)
        ProcedureReturn
    EndIf

    success = CkEmail::ckAddAttachmentBd(email,"photo.png",bdImage,"image/png")
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkBinData::ckDispose(bdImage)
        ProcedureReturn
    EndIf

    ;  Load the bytes of the first (index 0) image/png part into a BinData object.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckGetNthBinaryPartOfTypeBd(email,0,"image/png",0,0,bd)
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkBinData::ckDispose(bdImage)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "image/png part size (bytes) = " + Str(CkBinData::ckNumBytes(bd))

    ;  Note: The path "qa_data/images/photo.png" is a relative local filesystem path,
    ;  relative to the current working directory of the running application.


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


    ProcedureReturn
EndProcedure