Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

How to Download Messages in MessageSet One at a Time

See more IMAP Examples

If a message set contains a huge number of emails, it's NOT a good idea to try to download all at once into an email bundle using a method such as FetchBundle. It's better to iterate over the messages in the set to download one by one.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

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

// Connect using TLS.
imap.set_ssl(true);
imap.set_port(993);
if imap.connect("imap.example.com").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Authenticate
if imap.login("email_account_login", "email_account_password").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Select an IMAP mailbox
if imap.select_mailbox("Inbox").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Search for messages and return a set of matching messages.
// (This example will simply search for ALL messages.)
let fetch_uids = true;

let message_set = chilkat::MessageSet::new();
if imap.query_mbx("ALL", fetch_uids, &message_set).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

println!("Number of messages = {}", message_set.count());

let email = chilkat::Email::new();
let mut i = 0;
while i < message_set.count() {
    if imap.fetch_email(false, message_set.get_id(i), fetch_uids, &email).is_err() {
        println!("{}", imap.last_error_text());
        return;
    }

    println!("{}; {}", email.from(), email.subject());

    i = i + 1;
}

println!("OK");