Rust
Rust
Iterate MIME Parts of an Email
See more Email Object Examples
Demonstrates how to iterate over the MIME sub-parts of an email, and retrieve the content of each MIME sub-part body.Note: This example requires some new features added to Chilkat v9.5.0.95.
Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// See the following Chilkat post to Quickly Understand Email MIME
let email = chilkat::Email::new();
if email.load_eml("qa_data/eml/sample.eml").is_err() {
println!("Failed to load .eml");
return;
}
let sb_content_type = chilkat::StringBuilder::new();
let case_sensitive = false;
// Get the total number of non-multipart MIME sub-parts.
// (This is a simple way of iterating over all the MIME leaf parts regardless of the MIME tree structure)
let inline_only = false;
let exclude_attachments = false;
let search_spec = "*/*".to_string();
let num_parts = email.get_num_parts_of_type(&search_spec, inline_only, exclude_attachments);
let mut i = 0;
while i < num_parts {
// What is the Content-Type of this MIME part?
let _ = sb_content_type.append(&email.get_nth_content_type(i, &search_spec, inline_only, exclude_attachments).unwrap_or_default());
if sb_content_type.starts_with("text/", case_sensitive) {
// Get the text body of this MIME part.
let text_body = email.get_nth_text_part_of_type(i, &search_spec, inline_only, exclude_attachments).unwrap_or_default();
println!("Got text body for {}", sb_content_type.get_as_string().unwrap_or_default());
} else {
if sb_content_type.contents_equal("message/rfc822", case_sensitive) {
// If the Content-Type is message/rfc822, then the MIME body for this part contains a full embedded MIME messages.
// Your application could load it into a Chilkat email object and recursively process...
let attached_email = chilkat::Email::new();
let bd_mime = chilkat::BinData::new();
let _ = email.get_nth_binary_part_of_type_bd(i, &search_spec, inline_only, exclude_attachments, &bd_mime);
let _ = attached_email.set_from_mime_bd(&bd_mime);
// Now your app can recursively process the attachedEmail...
} else {
// Get the bytes of this MIME body part.
let bd = chilkat::BinData::new();
let _ = email.get_nth_binary_part_of_type_bd(i, &search_spec, inline_only, exclude_attachments, &bd);
println!("Got binary body for {} numBytes = {}", sb_content_type.get_as_string().unwrap_or_default(), bd.num_bytes());
}
}
sb_content_type.clear();
i = i + 1;
}