Sample code for 30+ languages & platforms
JavaScript

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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var email = new CkEmail();
email.Subject = "Set attachment filename";

email.AddStringAttachment("oldname.txt","Some notes.");
console.log("Filename before: " + email.GetAttachmentFilename(0));

//  Change the filename of the first attachment (index 0).
success = email.SetAttachmentFilename(0,"newname.txt");
if (success == false) {
    console.log(email.LastErrorText);
    return true;
}

console.log("Filename after: " + email.GetAttachmentFilename(0));