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

Reply to an email and preserve its thread

See more Email Object Examples

Create a reply to with-attachments.eml. Address it to Reply-To when supplied, or to the original author otherwise. Link the reply to the original Message-ID, replace its body with a short answer, and print the destination before saving the draft.

Chilkat Rust Downloads

Rust

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

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

let reply = chilkat::Email::new();
if original.to_reply(&reply).is_err() {
    println!("{}", original.last_error_text());
    return;
}

reply.set_from("Alex Rivera <alex@example.net>");
// This is a reply to the author, not a reply-all. Set the destination explicitly.
reply.clear_to();
reply.clear_cc();
reply.clear_bcc();
let mut destination = original.get_header_field("Reply-To").unwrap_or_default();
if destination == "" {
    destination = original.from();
}

if reply.add_multiple_to(&destination).is_err() {
    println!("{}", reply.last_error_text());
    return;
}

// Link this answer to its parent and preserve the earlier conversation references.
let parent_id = original.get_header_field("Message-ID").unwrap_or_default();
if parent_id != "" {
    reply.add_header_field("In-Reply-To", &parent_id);
    let refs = chilkat::StringBuilder::new();
    let _ = refs.append(&original.get_header_field("References").unwrap_or_default());
    if refs.length() > 0 {
        let _ = refs.append(" ");
    }

    let _ = refs.append(&parent_id);
    reply.add_header_field("References", &refs.get_as_string().unwrap_or_default());
}

// A short text reply has no use for the original inline artwork or nested correspondence.
reply.remove_attached_messages();
reply.remove_html_alternative();
reply.drop_related_items();
reply.set_text_body("Thank you for your message.", "text/plain");
if reply.num_to() < 1 {
    println!("Reply has no destination. Review the original sender headers.");
    return;
}

println!("Reply goes to: {}", reply.get_to_addr(0).unwrap_or_default());
println!("In-Reply-To: {}", reply.get_header_field("In-Reply-To").unwrap_or_default());

if reply.save_eml("reply.eml").is_err() {
    println!("{}", reply.last_error_text());
    return;
}

println!("Saved reply.eml.");