Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet

nSuccess := 0

//  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.

oImap := CreateObject("Chilkat.Imap")

oImap:Ssl := 1
oImap:Port := 993

nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Find the messages to copy (here, messages seen today, by UID).
oMsgSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("SINCE 15-Jul-2026", 1, oMsgSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

//  Copy them to the "Backup" mailbox.  The originals remain in the Inbox.
nSuccess := oImap:CopyMultiple(oMsgSet, "Backup")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

? "Copied " + Str(oMsgSet:Count) + " messages to Backup."

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

oImap:destroy()
oMsgSet:destroy()