Sample code for 30+ languages & platforms
Rust

Store One or More Flags on a Single Message

See more IMAP Examples

Demonstrates the Chilkat Imap.StoreFlags method, which sets or clears one or more flags on a single message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is a space-separated list of flag names, and the fourth is the value (1 to set, 0 to clear). This example applies the Seen and Flagged flags to one message by UID.

Background: StoreFlags is the single-message counterpart to SetFlags and lets you change several flags at once by listing them space-separated (without leading backslashes). Because the ID can be either a UID or a sequence number, the bUid argument must match how you obtained it — a UID from a UID search, or a sequence number from a sequence-based operation. UIDs are the safer choice since they remain valid as the mailbox changes.

Chilkat Rust Downloads

Rust

// Demonstrates the Imap.StoreFlags method, which sets or clears one or more flags on a single
// message identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID
// vs sequence number, the 3rd is a space-separated list of flag names, and the 4th is the
// value (1 to set, 0 to clear).

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;
}

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

// Get the UIDs of all messages.
let msg_set = chilkat::MessageSet::new();
if imap.query_mbx("ALL", true, &msg_set).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

if msg_set.count() > 0 {
    // Apply the Seen and Flagged flags to the first message.  bUid is true because the ID
    // came from a UID search.
    let uid = msg_set.get_id(0);
    if imap.store_flags(uid, true, "Seen Flagged", 1).is_err() {
        println!("{}", imap.last_error_text());
        return;
    }

    println!("Updated flags on UID {}", uid);
}

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