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

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

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("****", "****").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;
}

// Get the message IDs of all the emails in the mailbox
// We can choose to fetch UIDs or sequence numbers.
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;
}

// When downloading headers, each email object contains
// (obviously) the headers, but the body will be missing.
// Attachments will not be included.  However, it is
// possible to get information about the attachments
// as well as the complete size of the email.
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;
}

// Loop over the email objects and display information about each:
let email = chilkat::Email::new();
let mut i = 0;
let mut j = 0;
let num_emails = bundle.message_count();
while i < num_emails {
    let _ = bundle.email_at(i, &email);

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

    // Display the recipients:
    j = 0;
    while j < email.num_to() {
        println!("TO: {}, {}", email.get_to_name(j).unwrap_or_default(), email.get_to_addr(j).unwrap_or_default());
        j = j + 1;
    }

    j = 0;
    while j < email.num_cc() {
        println!("CC: {}, {}", email.get_cc_name(j).unwrap_or_default(), email.get_cc_addr(j).unwrap_or_default());
        j = j + 1;
    }

    // Show the total size of the email, including body and attachments:
    println!("{}", email.size());

    // When a full email is downloaded, we would use the
    // email.NumAttachments property in conjunction with the
    // email.GetMailAttach* methods.
    // However, when an email object contains only the header,
    // we need to access the attachment info differently:
    let num_attach = imap.get_mail_num_attach(&email);
    j = 0;
    while j < num_attach {
        println!("{}", imap.get_mail_attach_filename(&email, j).unwrap_or_default());
        let attach_size = imap.get_mail_attach_size(&email, j);
        println!("    size = {} bytes", attach_size);
        j = j + 1;
    }

    println!("--");

    i = i + 1;
}

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