Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    Boolean iSuccess
    Integer iTemp1

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

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Drop a single attachment"

    Get ComAddStringAttachment Of hoEmail "a.txt" "first attachment" To iSuccess
    Get ComAddStringAttachment Of hoEmail "b.txt" "second attachment" To iSuccess
    Get ComNumAttachments Of hoEmail To iTemp1
    Showln "NumAttachments before = " iTemp1

    //  Remove the attachment at index 0 (the first attachment).
    Get ComDropSingleAttachment Of hoEmail 0 To iSuccess

    Get ComNumAttachments Of hoEmail To iTemp1
    Showln "NumAttachments after = " iTemp1


End_Procedure