Sample code for 30+ languages & platforms
DataFlex

Save All Email Attachments to a Directory

See more Email Object Examples

Demonstrates the Chilkat Email.SaveAllAttachments method, which saves all of the email's attachments to a directory. If the directory or any of its components do not exist, Chilkat creates them automatically. This example adds two attachments and saves them.

Background: Each attachment is written using its own filename. Because those filenames come from the sender and may collide, consider the OverwriteExisting property (which can auto-generate unique names) and RemoveAttachmentPaths (which strips embedded path info) to keep saved files safe and non-clobbering — important when processing untrusted incoming mail.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the SaveAllAttachments method, which saves all attachments to a directory.
    //  If the directory (or any of its components) does not exist, Chilkat creates it.

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

    Get ComAddStringAttachment Of hoEmail "a.txt" "first attachment" To iSuccess
    Get ComAddStringAttachment Of hoEmail "b.txt" "second attachment" To iSuccess

    //  Save every attachment into the specified directory.
    Get ComSaveAllAttachments Of hoEmail "qa_output/attachments" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Saved all attachments."

    //  Note: The path "qa_output/..." is a relative local filesystem path,
    //  relative to the current working directory of the running application.


End_Procedure