Sample code for 30+ languages & platforms
Rust

Change the Filename of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentFilename method, which changes the filename of the attachment at a given zero-based index. This example adds an attachment and renames it, printing the filename before and after.

Background: The attachment filename is what a recipient sees and what most clients suggest when saving the file. Renaming it is handy when the original name is unclear, unsafe, or generic — for example giving a machine-generated tmp12345 a meaningful name like invoice.pdf before sending.

Chilkat Rust Downloads

Rust

// Demonstrates the SetAttachmentFilename method, which changes the filename of the
// attachment at the given zero-based index.

let email = chilkat::Email::new();
email.set_subject("Set attachment filename");

let _ = email.add_string_attachment("oldname.txt", "Some notes.");
println!("Filename before: {}", email.get_attachment_filename(0).unwrap_or_default());

// Change the filename of the first attachment (index 0).
if email.set_attachment_filename(0, "newname.txt").is_err() {
    println!("{}", email.last_error_text());
    return;
}

println!("Filename after: {}", email.get_attachment_filename(0).unwrap_or_default());