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

Create an email with plain-text and HTML bodies

See more Email Object Examples

Set the From and Reply-To addresses, add a recipient, and provide plain-text and HTML versions of the same body. Save the message as text-and-html.eml.

Chilkat Rust Downloads

Rust

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

let email = chilkat::Email::new();
email.set_from_name("Sam Lee");
email.set_from_address("sam@example.com");
email.set_reply_to("replies@example.com");
if email.add_to("Alex Rivera", "alex@example.net").is_err() {
    println!("{}", email.last_error_text());
    return;
}

email.set_subject("Sample message");
email.set_charset("utf-8");
// A custom header can carry a reference alongside the standard message fields.
email.add_header_field("X-Reference-ID", "REF-42");
if email.add_plain_text_alternative_body("Read more at https://example.com/details.").is_err() {
    println!("{}", email.last_error_text());
    return;
}

if email.add_html_alternative_body("<p><a href='https://example.com/details'>Read more</a></p>").is_err() {
    println!("{}", email.last_error_text());
    return;
}

if email.save_eml("text-and-html.eml").is_err() {
    println!("{}", email.last_error_text());
    return;
}

println!("Saved text-and-html.eml.");