Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkImapW.h>
#include <CkMessageSetW.h>
void ChilkatSample(void)
{
bool success = false;
// 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.
CkImapW imap;
imap.put_Ssl(true);
imap.put_Port(993);
success = imap.Connect(L"imap.example.com");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
success = imap.Login(L"user@example.com",L"myPassword");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
success = imap.SelectMailbox(L"Inbox");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Find the messages to move (here, messages from a particular sender, by UID).
CkMessageSetW msgSet;
success = imap.QueryMbx(L"FROM \"newsletter@example.com\"",true,msgSet);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Move them to the "Archive" mailbox.
success = imap.MoveMessages(msgSet,L"Archive");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
wprintf(L"Moved %d messages to Archive.\n",msgSet.get_Count());
success = imap.Disconnect();
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}