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

IMAP Download All Email One at a Time

Demonstrates how to download every email in an IMAP mailbox one at a time as a MIME string or as an email object. (The MIME contains the full contents of the email including all attachments.)

Chilkat Rust Downloads

Rust

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

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

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

// Login
if imap.login("myLogin", "myPassword").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;
}

// Once the mailbox is selected, the NumMessages property
// will contain the number of messages in the mailbox.
// You may loop from 1 to NumMessages to
// fetch each message by sequence number.

let b_uid = false;
let n = imap.num_messages();
for i in 1..=n {

    // Download the email by sequence number.
    let _ = imap.fetch_single_as_mime(i as u32, b_uid).unwrap_or_default();

    // ... your application may process each MIME string...
}

// An alternative is to download each email in the form of an
// email object, like this:
let email = chilkat::Email::new();
for i in 1..=n {

    // Download the email by sequence number.
    let _ = imap.fetch_email(false, i as u32, b_uid, &email).is_ok();

    // ... your application may process the email object...

}

// Disconnect from the IMAP server.
let _ = imap.disconnect().is_ok();