Unicode C++
Unicode C++
Set an IMAP Flag Using an Email Object
See more IMAP Examples
Demonstrates the Chilkat Imap.SetMailFlag method, which sets or clears a flag on the server for the message represented by an Email object. The first argument is the Email, the second is the flag name, and the third is the value (1 to set, 0 to clear). This example flags each fetched message as Flagged on the server.
Background:
SetMailFlag is convenient when you already hold an Email from a fetch: Chilkat uses the UID it recorded on that object to target the right message on the server, so you do not have to track IDs yourself. It updates both the server and the local Email object, keeping the two in sync. Under the hood this is the same STORE operation as SetFlag — just addressed by object rather than by raw ID.Chilkat Unicode C++ Downloads
#include <CkImapW.h>
#include <CkMessageSetW.h>
#include <CkEmailBundleW.h>
#include <CkEmailW.h>
void ChilkatSample(void)
{
bool success = false;
// Demonstrates the Imap.SetMailFlag method, which sets or clears a flag on the server for the
// message represented by an Email object. The 1st argument is the Email, the 2nd is the flag
// name, and the 3rd is the value (1 to set, 0 to clear).
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;
}
CkMessageSetW msgSet;
success = imap.QueryMbx(L"UNSEEN",true,msgSet);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
CkEmailBundleW bundle;
success = imap.FetchMsgSet(true,msgSet,bundle);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
CkEmailW email;
int i;
for (i = 0; i <= bundle.get_MessageCount() - 1; i++) {
success = bundle.EmailAt(i,email);
// Mark this message as Flagged on the server. A value of 1 sets the flag; 0 would clear it.
success = imap.SetMailFlag(email,L"Flagged",1);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}
wprintf(L"Flagged %d messages.\n",bundle.get_MessageCount());
success = imap.Disconnect();
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}