Sample code for 30+ languages & platforms
PureBasic

Save Email Attachments to Filesystem

Saves email attachments to a directory.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; Load an email object containing attachments.
    ; This .eml can be downloaded from:
    ; http://www.example-code.com/testData/HtmlEmail.eml

    success = CkEmail::ckLoadEml(email,"HtmlEmail.eml")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ; If OverwriteExisting is turned on, files with the same
    ; name are overwritten.  If turned off, new/unique filenames
    ; are automatically generated.  The filenames actually saved
    ; are accessible via the GetAttachmentFilename method.
    CkEmail::setCkOverwriteExisting(email, 1)

    ; Save all attachments to the "myAttachments" subdirectory
    ; found under the calling process's current working directory.
    ; This directory is automatically created if it does not already
    ; exist.
    success = CkEmail::ckSaveAllAttachments(email,"myAttachments")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ; List the attachment filenames:
    i.i
    For i = 0 To CkEmail::ckNumAttachments(email) - 1
        Debug CkEmail::ckGetAttachmentFilename(email,i)
    Next


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure