Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Set attachment filename"

    Get ComAddStringAttachment Of hoEmail "oldname.txt" "Some notes." To iSuccess
    Get ComGetAttachmentFilename Of hoEmail 0 To sTemp1
    Showln "Filename before: " sTemp1

    //  Change the filename of the first attachment (index 0).
    Get ComSetAttachmentFilename Of hoEmail 0 "newname.txt" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAttachmentFilename Of hoEmail 0 To sTemp1
    Showln "Filename after: " sTemp1


End_Procedure