Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkImap.h>
#include <C_CkMailboxes.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImap imap;
    BOOL subscribedOnly;
    HCkMailboxes mailboxes;
    int n;
    int i;
    const char *name;
    const char *flags;
    int numFlags;
    int j;
    const char *flag;
    int sentIndex;

    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.

    imap = CkImap_Create();

    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);

    success = CkImap_Connect(imap,"imap.example.com");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    success = CkImap_Login(imap,"user@example.com","myPassword");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    //  Populate a Mailboxes object with the full list of mailboxes.  The first argument requests
    //  all mailboxes (not subscribed-only) and "*" matches any name.
    subscribedOnly = FALSE;
    mailboxes = CkMailboxes_Create();
    success = CkImap_MbxList(imap,subscribedOnly,"","*",mailboxes);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMailboxes_Dispose(mailboxes);
        return;
    }

    //  Loop over each mailbox in the returned collection.
    n = CkMailboxes_getCount(mailboxes);
    printf("Number of mailboxes: %d\n",n);

    for (i = 0; i <= n - 1; i++) {
        name = CkMailboxes_getName(mailboxes,i);
        printf("Mailbox: %s\n",name);

        //  Whether the mailbox can be selected or examined for message access.
        if (CkMailboxes_IsSelectable(mailboxes,i)) {
            printf("  Selectable: yes\n");
        }
        else {
            printf("  Selectable: no\n");
        }

        //  Whether the server marked the mailbox as possibly having new messages.
        if (CkMailboxes_IsMarked(mailboxes,i)) {
            printf("  Marked: yes\n");
        }

        //  Whether the mailbox has, or may contain, child mailboxes.
        if (CkMailboxes_HasInferiors(mailboxes,i)) {
            printf("  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 (CkMailboxes_HasFlag(mailboxes,i,"\\HasChildren")) {
            printf("  Has child mailboxes\n");
        }

        //  Get all of the mailbox's flags as a comma-separated string.
        flags = CkMailboxes_getFlags(mailboxes,i);
        printf("  Flags: %s\n",flags);

        //  Iterate the flags individually.
        numFlags = CkMailboxes_GetNumFlags(mailboxes,i);

        for (j = 0; j <= numFlags - 1; j++) {
            flag = CkMailboxes_getNthFlag(mailboxes,i,j);
            printf("    Flag %d: %s\n",j,flag);
        }

    }

    //  Look up a mailbox by name.  Returns -1 if no mailbox with that name is present.
    sentIndex = CkMailboxes_GetMailboxIndex(mailboxes,"Sent");
    if (sentIndex >= 0) {
        printf("The Sent mailbox is at index %d\n",sentIndex);
    }

    success = CkImap_Disconnect(imap);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMailboxes_Dispose(mailboxes);
        return;
    }



    CkImap_Dispose(imap);
    CkMailboxes_Dispose(mailboxes);

    }