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

Fetch Single Email by UID or Sequence Number

Assuming the UID is known, download a single email by UID from an IMAP mail server.

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

let email = chilkat::Email::new();

let uid = 2014;
let is_uid = true;

if imap.fetch_email(false, uid as u32, is_uid, &email).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

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

// Display the Body property, which is the default body.
// If an email has an HTML body, the Body property contains
// the HTML source.  Otherwise it contains the plain-text
// body.
println!("---- EMAIL BODY ----");
println!("{}", email.body());
println!("--------------------");

// Display the recipients:
for j in 0..email.num_to() {
    println!("{}, {}", email.get_to_name(j).unwrap_or_default(), email.get_to_addr(j).unwrap_or_default());
}

for j in 0..email.num_cc() {
    println!("{}, {}", email.get_cc_name(j).unwrap_or_default(), email.get_cc_addr(j).unwrap_or_default());
}

// 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);
println!("{}", num_attach);

for j in 0..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);
}

println!("--");

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