Java
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
public class ChilkatExample {
public static void main(String[] args) {
// Demonstrates the SetAttachmentFilename method, which changes the filename of the
// attachment at the given zero-based index.
Email email = new Email();
email.setSubject("Set attachment filename");
email.addStringAttachment("oldname.txt", "Some notes.");
System.out.println("Filename before: " + email.getAttachmentFilename(0));
// Change the filename of the first attachment (index 0).
try {
email.setAttachmentFilename(0, "newname.txt");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Filename after: " + email.getAttachmentFilename(0));
}
}