Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 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.

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993

set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "user@example.com" "myPassword"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Find the messages to copy (here, messages seen today, by UID).
set msgSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "SINCE 15-Jul-2026" 1 $msgSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}

#  Copy them to the "Backup" mailbox.  The originals remain in the Inbox.
set success [CkImap_CopyMultiple $imap $msgSet "Backup"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}

puts "Copied [CkMessageSet_get_Count $msgSet] messages to Backup."

set success [CkImap_Disconnect $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}


delete_CkImap $imap
delete_CkMessageSet $msgSet