Unicode C++
Unicode C++
Search an IMAP Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.QueryMbx method, which searches the selected mailbox using IMAP search criteria and stores the matching message identifiers in a MessageSet. If bUid is true, the identifiers are UIDs; otherwise they are sequence numbers. This example finds unseen messages and lists their UIDs.
Background: IMAP search runs on the server, so you can find matching messages without downloading them. The criteria string is standard IMAP search syntax — for example
UNSEEN, FROM "alice@example.com", SUBJECT "invoice", or SINCE 1-Jan-2026 — and terms can be combined. The result is a MessageSet of identifiers you then fetch, flag, copy, or move. Requesting UIDs (bUid = true) is preferred because UIDs remain valid across sessions.Chilkat Unicode C++ Downloads
#include <CkImapW.h>
#include <CkMessageSetW.h>
void ChilkatSample(void)
{
bool success = false;
// Demonstrates the Imap.QueryMbx method, which searches the selected mailbox using IMAP
// search criteria and stores the matching message identifiers in a MessageSet. If bUid is
// true, the identifiers are UIDs; otherwise they are sequence numbers.
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;
}
// Search for unseen (unread) messages, returning their UIDs.
CkMessageSetW msgSet;
success = imap.QueryMbx(L"UNSEEN",true,msgSet);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Iterate the matching message identifiers.
int n = msgSet.get_Count();
wprintf(L"Matching messages: %d\n",n);
int i;
for (i = 0; i <= n - 1; i++) {
wprintf(L"UID: %u\n",msgSet.GetId(i));
}
success = imap.Disconnect();
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}