Sample code for 30+ languages & platforms
Tcl

Move IMAP Messages to Another Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.MoveMessages method, which moves the messages in a MessageSet to another mailbox on the server. The first argument is the MessageSet and the second is the destination folder name. This example searches for newsletter messages and moves them to the Archive mailbox.

Background: A true server-side move (the IMAP MOVE extension) is atomic and efficient: the message never leaves the server, so no download/re-upload is involved. If the server lacks the MOVE capability, the equivalent is copy-then-delete-then-expunge — Chilkat handles that fallback for you, but the destination mailbox must already exist.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.MoveMessages method, which moves the messages in a MessageSet to
#  another mailbox on the server.  The 1st argument is the MessageSet and the 2nd is the
#  destination folder 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 move (here, messages from a particular sender, by UID).
set msgSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "FROM \"newsletter@example.com\"" 1 $msgSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}

#  Move them to the "Archive" mailbox.
set success [CkImap_MoveMessages $imap $msgSet "Archive"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}

puts "Moved [CkMessageSet_get_Count $msgSet] messages to Archive."

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