Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "GetNthBinaryPartOfTypeBd example"
    Set ComBody Of hoEmail To "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).
    Get Create (RefClass(cComChilkatBinData)) To hoBdImage
    If (Not(IsComObjectCreated(hoBdImage))) Begin
        Send CreateComObject of hoBdImage
    End
    Get ComLoadFile Of hoBdImage "qa_data/images/photo.png" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoBdImage To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoBdImage to vBdImage
    Get ComAddAttachmentBd Of hoEmail "photo.png" vBdImage "image/png" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load the bytes of the first (index 0) image/png part into a BinData object.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get pvComObject of hoBd to vBd
    Get ComGetNthBinaryPartOfTypeBd Of hoEmail 0 "image/png" False False vBd To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumBytes Of hoBd To iTemp1
    Showln "image/png part size (bytes) = " iTemp1

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


End_Procedure