Sample code for 30+ languages & platforms
DataFlex

Fetch a Chunk of Messages, Tracking Failures

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchChunk2 method, which downloads a run of messages by sequence number and reports which succeeded and which failed. The arguments are the starting sequence number, the count, a MessageSet that receives the failed IDs, a MessageSet that receives the successfully-fetched IDs, and the EmailBundle. This example fetches the first chunk of messages and reports the success and failure counts.

Background: When bulk-downloading a large mailbox, individual messages can occasionally fail to fetch (a corrupt message on the server, a transient error) without failing the whole operation. FetchChunk2 is designed for that reality: it returns a partial EmailBundle plus two MessageSet objects so your code can log or retry exactly the messages that did not come through, rather than aborting the entire sync.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Integer iNumMessages
    Integer iCount
    Variant vFailedSet
    Handle hoFailedSet
    Variant vFetchedSet
    Handle hoFetchedSet
    Variant vBundle
    Handle hoBundle
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.FetchChunk2 method, which downloads a run of messages by sequence
    //  number and reports which succeeded and which failed.  The 1st argument is the starting
    //  sequence number, the 2nd is the count, the 3rd receives the failed IDs, the 4th receives
    //  the successfully-fetched IDs, and the 5th is the EmailBundle.

    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 iNumMessages
    If (iNumMessages < 0) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Fetch messages 1..25 (or fewer if the mailbox is smaller).
    Move 25 To iCount
    If (iCount > iNumMessages) Begin
        Move iNumMessages To iCount
    End

    Get Create (RefClass(cComChilkatMessageSet)) To hoFailedSet
    If (Not(IsComObjectCreated(hoFailedSet))) Begin
        Send CreateComObject of hoFailedSet
    End
    Get Create (RefClass(cComChilkatMessageSet)) To hoFetchedSet
    If (Not(IsComObjectCreated(hoFetchedSet))) Begin
        Send CreateComObject of hoFetchedSet
    End
    Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle
    If (Not(IsComObjectCreated(hoBundle))) Begin
        Send CreateComObject of hoBundle
    End
    Get pvComObject of hoFailedSet to vFailedSet
    Get pvComObject of hoFetchedSet to vFetchedSet
    Get pvComObject of hoBundle to vBundle
    Get ComFetchChunk2 Of hoImap 1 iCount vFailedSet vFetchedSet vBundle To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCount Of hoFetchedSet To iTemp1
    Showln "Fetched: " iTemp1
    Get ComCount Of hoFailedSet To iTemp1
    Showln "Failed:  " iTemp1

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



End_Procedure