Sample code for 30+ languages & platforms
PowerBuilder

Save All MIME Attachments to Files

See more MIME Examples

Demonstrates the Chilkat Mime.PartsToFiles method, which recursively traverses the MIME tree and saves each part having a nonempty filename parameter to a directory. The first argument is the destination directory and the second is a StringTable that receives the saved filenames.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This extracts every named attachment from a message in one call, walking the whole tree so it finds attachments nested inside multipart containers. It saves only parts that declare a filename — inline body parts without one are skipped — and reports what it wrote via the StringTable, which is handy for logging or follow-up processing. The destination directory must already exist.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mime
oleobject loo_SavedFiles
integer n
integer i
string ls_Name

li_Success = 0

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
if li_rc < 0 then
    destroy loo_Mime
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Mime.LoadMimeFile("qa_data/multipart_message.eml")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  Recursively traverse the MIME tree and save each part that has a nonempty "filename" parameter
//  to a directory.  The 1st argument is the destination directory and the 2nd is a StringTable
//  that receives the saved filenames.
loo_SavedFiles = create oleobject
li_rc = loo_SavedFiles.ConnectToNewObject("Chilkat.StringTable")

li_Success = loo_Mime.PartsToFiles("qa_output/attachments",loo_SavedFiles)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_SavedFiles
    return
end if

n = loo_SavedFiles.Count
Write-Debug "Saved " + string(n) + " files:"

for i = 0 to n - 1
    ls_Name = loo_SavedFiles.StringAt(i)
    if loo_SavedFiles.LastMethodSuccess = 0 then
        Write-Debug loo_SavedFiles.LastErrorText
        destroy loo_Mime
        destroy loo_SavedFiles
        return
    end if

    Write-Debug ls_Name
next


destroy loo_Mime
destroy loo_SavedFiles