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

Fetch Oldest/Newest IMAP Email

Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.

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("mail.testemail.net").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

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

// After selecting a mailbox, the NumMessages property
// contains the number of emails in the selected mailbox.
let n = imap.num_messages();

if n > 0 {

    // The oldest email is always at sequence number 1.
    let is_uid = false;

    let oldest_email = chilkat::Email::new();
    if imap.fetch_email(false, 1, is_uid, &oldest_email).is_err() {
        println!("{}", imap.last_error_text());
        return;
    }

    // Display the From and Subject
    println!("{}", oldest_email.from_address());
    println!("{}", oldest_email.subject());

    println!("--");

    // The newest email is at sequence number N:
    let newest_email = chilkat::Email::new();
    if imap.fetch_email(false, n as u32, is_uid, &newest_email).is_err() {
        println!("{}", imap.last_error_text());
        return;
    }

    // Display the From and Subject
    println!("{}", newest_email.from_address());
    println!("{}", newest_email.subject());

}

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