Sample code for 30+ languages & platforms
PowerBuilder

Remove a Single Attachment from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.DropSingleAttachment method, which removes the attachment at the specified zero-based index. Valid indexes run from 0 through NumAttachments - 1. This example adds two attachments, removes the first, and prints the count before and after.

Background: Attachments are addressed by a zero-based index, so the first is 0, the second 1, and so on. DropSingleAttachment removes just one — use it to prune a specific file (say, an oversized or unwanted attachment) while keeping the rest. Note that after a removal the remaining attachments shift down, so if you are deleting several by index, iterate from the highest index downward to avoid skipping any.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
oleobject loo_Email

//  Demonstrates the DropSingleAttachment method, which removes the attachment at the
//  specified zero-based index.

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 = "Drop a single attachment"

loo_Email.AddStringAttachment("a.txt","first attachment")
loo_Email.AddStringAttachment("b.txt","second attachment")
Write-Debug "NumAttachments before = " + string(loo_Email.NumAttachments)

//  Remove the attachment at index 0 (the first attachment).
loo_Email.DropSingleAttachment(0)

Write-Debug "NumAttachments after = " + string(loo_Email.NumAttachments)


destroy loo_Email