Sample code for 30+ languages & platforms
DataFlex

Copy a Range of IMAP Messages

See more IMAP Examples

Demonstrates the Chilkat Imap.CopySequence method, which copies a contiguous range of messages, identified by sequence number, from the selected mailbox to a destination mailbox. The first argument is the starting sequence number and the second is the number of messages to copy. This example copies the first ten messages to another mailbox.

Background: Copying a whole range in a single command is far more efficient than copying messages one at a time — it's one server round trip instead of many. This method works by sequence number (position), so it is well suited to "copy the first N" or "copy messages 50–100" style operations. To copy an arbitrary, non-contiguous set of messages, build a MessageSet and use CopyMultiple.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.CopySequence method, which copies a contiguous range of messages,
    //  identified by sequence number, from the selected mailbox to a destination mailbox.  The
    //  1st argument is the first sequence number and the 2nd is the number of messages to copy.

    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

    //  Copy the first 10 messages (sequence numbers 1 through 10) to "Archive2026".
    Get ComCopySequence Of hoImap 1 10 "Archive2026" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Copied the message range."

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



End_Procedure