Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

// 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.

let imap = chilkat::Imap::new();

imap.set_ssl(true);
imap.set_port(993);

if imap.connect("imap.example.com").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

if imap.login("user@example.com", "myPassword").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Populate a Mailboxes object with the full list of mailboxes.  The first argument requests
// all mailboxes (not subscribed-only) and "*" matches any name.
let subscribed_only = false;
let mailboxes = chilkat::Mailboxes::new();
if imap.mbx_list(subscribed_only, "", "*", &mailboxes).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Loop over each mailbox in the returned collection.
let n = mailboxes.count();
println!("Number of mailboxes: {}", n);

for i in 0..n {
    let name = mailboxes.get_name(i).unwrap_or_default();
    println!("Mailbox: {}", name);

    // Whether the mailbox can be selected or examined for message access.
    if mailboxes.is_selectable(i) {
        println!("  Selectable: yes");
    } else {
        println!("  Selectable: no");
    }

    // Whether the server marked the mailbox as possibly having new messages.
    if mailboxes.is_marked(i) {
        println!("  Marked: yes");
    }

    // Whether the mailbox has, or may contain, child mailboxes.
    if mailboxes.has_inferiors(i) {
        println!("  May have child mailboxes");
    }

    // 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.has_flag(i, "\\HasChildren") {
        println!("  Has child mailboxes");
    }

    // Get all of the mailbox's flags as a comma-separated string.
    let flags = mailboxes.get_flags(i).unwrap_or_default();
    println!("  Flags: {}", flags);

    // Iterate the flags individually.
    let num_flags = mailboxes.get_num_flags(i);
    for j in 0..num_flags {
        let flag = mailboxes.get_nth_flag(i, j).unwrap_or_default();
        println!("    Flag {}: {}", j, flag);
    }

}

// Look up a mailbox by name.  Returns -1 if no mailbox with that name is present.
let sent_index = mailboxes.get_mailbox_index("Sent");
if sent_index >= 0 {
    println!("The Sent mailbox is at index {}", sent_index);
}

if imap.disconnect().is_err() {
    println!("{}", imap.last_error_text());
    return;
}