Sample code for 30+ languages & platforms
DataFlex

Remove All Attachments from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.DropAttachments method, which removes all ordinary attachments from the in-memory message. Related items (inline images, style sheets) and attached message/rfc822 emails are not affected. This example adds two attachments, drops them, and prints the count before and after.

Background: Chilkat distinguishes three kinds of enclosed content: ordinary attachments (files meant to be saved), related items (resources that support the HTML display), and nested attached messages. DropAttachments targets only the first category — useful, for example, when forwarding or re-sending a message body without carrying its original file attachments along.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    Boolean iSuccess
    Integer iTemp1

    //  Demonstrates the DropAttachments method, which removes all ordinary attachments from
    //  the email.  Related items and attached messages are not affected.

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

    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 all attachments.
    Send ComDropAttachments To hoEmail

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


End_Procedure