Sample code for 30+ languages & platforms
DataFlex

Copy Multiple IMAP Messages to Another Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.CopyMultiple method, which copies the messages in a MessageSet to another mailbox, leaving the originals in place. The first argument is the MessageSet and the second is the destination mailbox name. This example searches for recent messages and copies them to a Backup mailbox.

Background: This maps to the IMAP COPY command, which duplicates messages entirely on the server — nothing is downloaded. The copy in the destination mailbox is an independent message with its own UID; changing flags on one copy does not affect the other. Combine CopyMultiple with a search from QueryMbx to snapshot or archive matching messages without removing them from their current folder.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Variant vMsgSet
    Handle hoMsgSet
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.CopyMultiple method, which copies the messages in a MessageSet to
    //  another mailbox, leaving the originals in place.  The 1st argument is the MessageSet and
    //  the 2nd is the destination mailbox name.

    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 copy (here, messages seen today, 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 "SINCE 15-Jul-2026" True vMsgSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Copy them to the "Backup" mailbox.  The originals remain in the Inbox.
    Get pvComObject of hoMsgSet to vMsgSet
    Get ComCopyMultiple Of hoImap vMsgSet "Backup" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCount Of hoMsgSet To iTemp1
    Showln "Copied " iTemp1 " messages to Backup."

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



End_Procedure