Sample code for 30+ languages & platforms
DataFlex

Fetch a MessageSet into an EmailBundle

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.

Background: This is the natural pairing with QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Variant vMsgSet
    Handle hoMsgSet
    Variant vBundle
    Handle hoBundle
    Integer n
    Variant vEmail
    Handle hoEmail
    Integer i
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
    //  MessageSet and appends them to an EmailBundle.  The 1st argument is headersOnly, the 2nd
    //  is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).

    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

    //  Find the messages to download (here, the unseen messages by UID).
    Get Create (RefClass(cComChilkatMessageSet)) To hoMsgSet
    If (Not(IsComObjectCreated(hoMsgSet))) Begin
        Send CreateComObject of hoMsgSet
    End
    Get pvComObject of hoMsgSet to vMsgSet
    Get ComQueryMbx Of hoImap "UNSEEN" True vMsgSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Download the messages in the set (full bodies) into a bundle.
    Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle
    If (Not(IsComObjectCreated(hoBundle))) Begin
        Send CreateComObject of hoBundle
    End
    Get pvComObject of hoMsgSet to vMsgSet
    Get pvComObject of hoBundle to vBundle
    Get ComFetchMsgSet Of hoImap False vMsgSet vBundle To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComMessageCount Of hoBundle To n
    Showln "Fetched " n " messages."
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    For i From 0 To (n - 1)
        Get pvComObject of hoEmail to vEmail
        Get ComEmailAt Of hoBundle i vEmail To iSuccess
        Get ComSubject Of hoEmail To sTemp1
        Showln sTemp1
    Loop

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



End_Procedure