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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat Rust Downloads

Rust

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

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

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

// The NumMessages property contains the number of messages in the selected mailbox.
let num_messages = imap.num_messages();
if num_messages == 0 {
    println!("No messages exist in the Inbox.");
    return;
}

let sb_mime = chilkat::StringBuilder::new();
for seq_num in 1..=num_messages {
    sb_mime.clear();
    if imap.fetch_single_as_mime_sb(seq_num as u32, false, &sb_mime).is_err() {
        println!("{}", imap.last_error_text());
        return;
    }

    // The MIME source of the downloaded email is contained in sbMime.
}

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