Unicode C++
Unicode C++
List and Inspect IMAP Mailboxes
See more IMAP Examples
Demonstrates the Chilkat Mailboxes class, a read-only result collection populated by Imap.MbxList. This example connects to an IMAP server, lists all mailboxes into a Mailboxes object, then loops from 0 to Count - 1 and inspects each mailbox: its name (GetName), whether it is selectable (IsSelectable) or marked (IsMarked), whether it may have child mailboxes (HasInferiors), a specific flag (HasFlag), and all of its flags both as a string (GetFlags) and individually (GetNumFlags with GetNthFlag). Finally it looks up a mailbox by name with GetMailboxIndex.
Background:
Mailboxes is how an application renders an IMAP folder tree. Because it is read-only, you never construct it directly — you receive it from MbxList and read its entries by index. Mailbox names come back as normal Unicode text (Chilkat handles the modified UTF-7 / IMAP UTF-8 encoding at the protocol level), so a name from GetName can be passed straight to methods like SelectMailbox. Note that HasInferiors is often true: it means children exist or may be created, which is not the same as \HasChildren.Chilkat Unicode C++ Downloads
#include <CkImapW.h>
#include <CkMailboxesW.h>
void ChilkatSample(void)
{
bool success = false;
// Demonstrates using Chilkat.Imap to populate a Mailboxes object (via MbxList), then looping
// over the collection to inspect each mailbox's name and attributes. Mailboxes is a read-only
// result collection -- applications inspect the entries but do not add, remove, or modify them.
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;
}
// Populate a Mailboxes object with the full list of mailboxes. The first argument requests
// all mailboxes (not subscribed-only) and "*" matches any name.
bool subscribedOnly = false;
CkMailboxesW mailboxes;
success = imap.MbxList(subscribedOnly,L"",L"*",mailboxes);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Loop over each mailbox in the returned collection.
int n = mailboxes.get_Count();
wprintf(L"Number of mailboxes: %d\n",n);
int i;
for (i = 0; i <= n - 1; i++) {
const wchar_t *name = mailboxes.getName(i);
wprintf(L"Mailbox: %s\n",name);
// Whether the mailbox can be selected or examined for message access.
if (mailboxes.IsSelectable(i)) {
wprintf(L" Selectable: yes\n");
}
else {
wprintf(L" Selectable: no\n");
}
// Whether the server marked the mailbox as possibly having new messages.
if (mailboxes.IsMarked(i)) {
wprintf(L" Marked: yes\n");
}
// Whether the mailbox has, or may contain, child mailboxes.
if (mailboxes.HasInferiors(i)) {
wprintf(L" May have child mailboxes\n");
}
// Check for a specific flag by name. Include the leading backslash for a system flag;
// in CkScript a literal backslash in a string is written as "\\".
if (mailboxes.HasFlag(i,L"\\HasChildren")) {
wprintf(L" Has child mailboxes\n");
}
// Get all of the mailbox's flags as a comma-separated string.
const wchar_t *flags = mailboxes.getFlags(i);
wprintf(L" Flags: %s\n",flags);
// Iterate the flags individually.
int numFlags = mailboxes.GetNumFlags(i);
int j;
for (j = 0; j <= numFlags - 1; j++) {
const wchar_t *flag = mailboxes.getNthFlag(i,j);
wprintf(L" Flag %d: %s\n",j,flag);
}
}
// Look up a mailbox by name. Returns -1 if no mailbox with that name is present.
int sentIndex = mailboxes.GetMailboxIndex(L"Sent");
if (sentIndex >= 0) {
wprintf(L"The Sent mailbox is at index %d\n",sentIndex);
}
success = imap.Disconnect();
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}