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

Sorting Email

Demonstrates how to sort an email bundle.

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

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

// Fetch the email headers into a bundle object:
let bundle = chilkat::EmailBundle::new();
let headers_only = true;
if imap.fetch_msg_set(headers_only, &message_set, &bundle).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// Sort the email bundle by date, recipient, sender, or subject:
let ascending = true;
bundle.sort_by_date(ascending);

// To sort by recipient, sender, or subject, call 
// SortBySender, SortByRecipient, or SortBySubject.

// Display the Subject and From of each email.
let email = chilkat::Email::new();
let mut i = 0;
while i < bundle.message_count() {
    let _ = bundle.email_at(i, &email);

    println!("{}", email.get_header_field("Date").unwrap_or_default());
    println!("{}", email.subject());
    println!("{}", email.from());
    println!("--");

    i = i + 1;
}

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