Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email

li_Success = 0

//  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.

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Email.Subject = "Save all attachments"

loo_Email.AddStringAttachment("a.txt","first attachment")
loo_Email.AddStringAttachment("b.txt","second attachment")

//  Save every attachment into the specified directory.
li_Success = loo_Email.SaveAllAttachments("qa_output/attachments")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

Write-Debug "Saved all attachments."

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


destroy loo_Email