Sample code for 30+ languages & platforms
DataFlex

Remove All Attached Messages from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachedMessages method, which removes all message/rfc822 sub-parts (attached emails) from the object at once. Ordinary file attachments and related HTML resources are left unchanged. This example attaches two emails, removes them all, and prints the count before and after.

Background: This is the "remove them all" companion to RemoveAttachedMessage. Because it touches only the nested-message parts, you can strip forwarded originals or a digest's members while preserving the message body, its inline images, and any regular attachments — a clean way to keep the carrier message while discarding what it enclosed.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Variant vInner1
    Handle hoInner1
    Variant vInner2
    Handle hoInner2
    Handle hoEmail
    Boolean iSuccess
    Integer iTemp1

    //  Demonstrates the RemoveAttachedMessages method, which removes all message/rfc822
    //  sub-parts (attached emails) from the email at once.  Ordinary file attachments and
    //  related items are not affected.

    Get Create (RefClass(cComChilkatEmail)) To hoInner1
    If (Not(IsComObjectCreated(hoInner1))) Begin
        Send CreateComObject of hoInner1
    End
    Set ComSubject Of hoInner1 To "Embedded 1"

    Get Create (RefClass(cComChilkatEmail)) To hoInner2
    If (Not(IsComObjectCreated(hoInner2))) Begin
        Send CreateComObject of hoInner2
    End
    Set ComSubject Of hoInner2 To "Embedded 2"

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Has attached messages"
    Get pvComObject of hoInner1 to vInner1
    Get ComAttachEmail Of hoEmail vInner1 To iSuccess
    Get pvComObject of hoInner2 to vInner2
    Get ComAttachEmail Of hoEmail vInner2 To iSuccess

    Get ComNumAttachedMessages Of hoEmail To iTemp1
    Showln "NumAttachedMessages before = " iTemp1

    //  Remove all attached messages.
    Send ComRemoveAttachedMessages To hoEmail

    Get ComNumAttachedMessages Of hoEmail To iTemp1
    Showln "NumAttachedMessages after = " iTemp1


End_Procedure