Sample code for 30+ languages & platforms
DataFlex

Fetch an IMAP Message's MIME into a BinData

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleBd method, which downloads one message's MIME bytes into a BinData object. If the second argument (bUid) is true, the first argument is a UID; otherwise it is a sequence number. This example downloads message 1's MIME into a BinData and prints the byte count.

Background: This is the binary counterpart to FetchSingleAsMime (which returns a string). A BinData holds raw bytes, so it preserves the message exactly — the right choice when you plan to write the MIME straight to a file, hash it, or hand it to an API expecting a byte buffer, with no charset conversion that could alter binary content.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Variant vBdMime
    Handle hoBdMime
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.FetchSingleBd method, which downloads one message's MIME bytes into a
    //  BinData object.  If the 2nd argument (bUid) is True, the 1st argument is a UID; otherwise
    //  it is a sequence number.

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Download message sequence number 1's MIME bytes into a BinData.  bUid = False.
    Get Create (RefClass(cComChilkatBinData)) To hoBdMime
    If (Not(IsComObjectCreated(hoBdMime))) Begin
        Send CreateComObject of hoBdMime
    End
    Get pvComObject of hoBdMime to vBdMime
    Get ComFetchSingleBd Of hoImap 1 False vBdMime To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumBytes Of hoBdMime To iTemp1
    Showln "MIME size (bytes) = " iTemp1

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure