Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL loMsgSet

lnSuccess = 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.

loImap = CreateObject('Chilkat.Imap')

loImap.Ssl = 1
loImap.Port = 993

lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("user@example.com","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

*  Find the messages to copy (here, messages seen today, by UID).
loMsgSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("SINCE 15-Jul-2026",1,loMsgSet)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    CANCEL
ENDIF

*  Copy them to the "Backup" mailbox.  The originals remain in the Inbox.
lnSuccess = loImap.CopyMultiple(loMsgSet,"Backup")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    CANCEL
ENDIF

? "Copied " + STR(loMsgSet.Count) + " messages to Backup."

lnSuccess = loImap.Disconnect()
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    CANCEL
ENDIF

RELEASE loImap
RELEASE loMsgSet