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

Load .eml and Examine the Structure, Attachments, and Related Items

See more Email Object Examples

Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let eml_path = "C:/AAWorkarea/beatrix/roesner.eml".to_string();

let mime = chilkat::Mime::new();

if mime.load_mime_file(&eml_path).is_err() {
    println!("{}", mime.last_error_text());
    return;
}

println!("---- MIME structure ----");
println!("{}", mime.get_structure("text").unwrap_or_default());
println!("------------------------");

let email = chilkat::Email::new();
let _ = email.load_eml(&eml_path).is_ok();

// Was this a signed and/or encrypted email?
// If so, then loading the .eml automatically unwraps
// (i.e. verifies signatures and decrypts) and the resultant
// email is what existed prior to signing/encrypting.
println!("Email was Signed: {}", email.received_signed());
println!("Email was Encrypted: {}", email.received_encrypted());
if email.received_signed() {
    println!("Signature(s) valid = {}", email.signatures_valid());
}

if email.received_encrypted() {
    println!("Decrypted successfully = {}", email.decrypted());
}

let mut i = 0;
let num_attach = email.num_attachments();
println!("Number of attachments = {}", num_attach);

while i < num_attach {
    println!("---- Attachment {}", i);

    // Examine the filename (if any)
    println!("filename: {}", email.get_attachment_filename(i).unwrap_or_default());
    // Examine the content-ID (if any)
    println!("Content-ID: {}", email.get_attachment_content_id(i).unwrap_or_default());
    // Examine the content-type
    println!("Content-Type: {}", email.get_attachment_content_type(i).unwrap_or_default());
    // Examine the content-disposition
    println!("Content-Disposition{}", email.get_attachment_header(i, "content-disposition").unwrap_or_default());
    // Examine the attachment size:
    println!("Size (in bytes) of the attachment: {}", email.get_attachment_size(i));

    i = i + 1;
}

println!("--");

// Now for the related items.

// Note: A MIME sub-part can potentially be both a related item AND an attachment.
// The typical case is when the item is contained under the multipart/related enclosure and 
// the item also has a "Content-Disposition" header indicating "attachment".
// The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
// Related items and attachments are not necessarily mutually exclusive.

let num_related = email.num_related_items();
println!("Number of related items = {}", num_related);
i = 0;
while i < num_related {
    println!("---- Related Item {}", i);

    // Examine the filename (if any)
    println!("filename: {}", email.get_related_filename(i).unwrap_or_default());
    // Examine the content-ID (if any)
    println!("Content-ID: {}", email.get_related_content_id(i).unwrap_or_default());
    // Examine the content-type
    println!("Content-Type: {}", email.get_related_content_type(i).unwrap_or_default());
    // Examine the content-location (if any)
    println!("Content-Location{}", email.get_related_content_location(i).unwrap_or_default());

    i = i + 1;
}

// The email could also have attached messages.
// An attached message is another email that was attached to this email.
let em = chilkat::Email::new();
let num_attached_messages = email.num_attached_messages();
println!("Number of attached messages = {}", num_attached_messages);
i = 0;
while i < num_attached_messages {
    println!("---- Attached message {}", i);

    // Examine the attached email
    let _ = email.get_attached_email(i, &em);
    println!("from: {}", em.from());
    println!("subject: {}", em.subject());
    i = i + 1;
}

// An email could also be a multipart/report email. 
// This is a DSN (Delivery Status Notification)
// The NumReports property indicates how many "reports" exist.
let num_reports = email.num_reports();
println!("Number of reports = {}", num_reports);
i = 0;
while i < num_reports {
    println!("---- Report {}", i);
    // Get the raw report data...
    println!("{}", email.get_report(i).unwrap_or_default());
    i = i + 1;
}

// If the email is a multipart/report, then the information
// from the message/delivery-status part of the email can be retrieved:
if email.is_multipart_report() {

    println!("--- Delivery Status Information:");
    println!("Status: {}", email.get_delivery_status_info("Status").unwrap_or_default());
    println!("Action: {}", email.get_delivery_status_info("Action").unwrap_or_default());
    println!("Reporting-MTA: {}", email.get_delivery_status_info("Reporting-MTA").unwrap_or_default());

    let json_dsn_info = chilkat::JsonObject::new();
    let _ = email.get_dsn_info(&json_dsn_info);
    json_dsn_info.set_emit_compact(false);
    println!("{}", json_dsn_info.emit().unwrap_or_default());
}