Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Set attachment filename")

    CkEmail::ckAddStringAttachment(email,"oldname.txt","Some notes.")
    Debug "Filename before: " + CkEmail::ckGetAttachmentFilename(email,0)

    ;  Change the filename of the first attachment (index 0).
    success = CkEmail::ckSetAttachmentFilename(email,0,"newname.txt")
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "Filename after: " + CkEmail::ckGetAttachmentFilename(email,0)


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure