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 <C_CkImapW.h>
#include <C_CkMessageSetW.h>
void ChilkatSample(void)
{
BOOL success;
HCkImapW imap;
HCkMessageSetW msgSet;
int n;
int i;
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.
imap = CkImapW_Create();
CkImapW_putSsl(imap,TRUE);
CkImapW_putPort(imap,993);
success = CkImapW_Connect(imap,L"imap.example.com");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
success = CkImapW_SelectMailbox(imap,L"Inbox");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// Search for unseen (unread) messages, returning their UIDs.
msgSet = CkMessageSetW_Create();
success = CkImapW_QueryMbx(imap,L"UNSEEN",TRUE,msgSet);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(msgSet);
return;
}
// Iterate the matching message identifiers.
n = CkMessageSetW_getCount(msgSet);
wprintf(L"Matching messages: %d\n",n);
for (i = 0; i <= n - 1; i++) {
wprintf(L"UID: %u\n",CkMessageSetW_GetId(msgSet,i));
}
success = CkImapW_Disconnect(imap);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(msgSet);
return;
}
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(msgSet);
}