Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet
nSuccess := 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.
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 move (here, messages from a particular sender, by UID).
oMsgSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx('FROM "newsletter@example.com"', 1, oMsgSet)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
RETURN
ENDIF
// Move them to the "Archive" mailbox.
nSuccess := oImap:MoveMessages(oMsgSet, "Archive")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
RETURN
ENDIF
? "Moved " + Str(oMsgSet:Count) + " messages to Archive."
nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
RETURN
ENDIF
oImap:destroy()
oMsgSet:destroy()