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

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat Rust Downloads

Rust

// Assumes Chilkat is unlocked. See Global Unlock Sample.

let email = chilkat::Email::new();
if email.load_eml("with-attachments.eml").is_err() {
    println!("{}", email.last_error_text());
    return;
}

println!("From: {}", email.from());
println!("Subject: {}", email.subject());
println!("Date: {}", email.email_date_str());
let mut i = 0;
while i < email.num_to() {
    println!("To: {}  {}", email.get_to_name(i).unwrap_or_default(), email.get_to_addr(i).unwrap_or_default());
    i = i + 1;
}

if let Ok(reference_id) = email.get_header_field("X-Reference-ID") {
    println!("Reference: {}", reference_id);
}

if email.has_plain_text_body() {
    println!("{}", email.get_plain_text_body().unwrap_or_default());
} else {
    if email.has_html_body() {
        let reader = chilkat::HtmlToText::new();
        println!("{}", reader.to_text(&email.get_html_body().unwrap_or_default()).unwrap_or_default());
    } else {
        println!("No text or HTML body.");
    }

}