Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

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

    inner1.i = CkEmail::ckCreate()
    If inner1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(inner1, "Embedded 1")

    inner2.i = CkEmail::ckCreate()
    If inner2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(inner2, "Embedded 2")

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Has attached messages")
    CkEmail::ckAttachEmail(email,inner1)
    CkEmail::ckAttachEmail(email,inner2)

    Debug "NumAttachedMessages before = " + Str(CkEmail::ckNumAttachedMessages(email))

    ;  Remove all attached messages.
    CkEmail::ckRemoveAttachedMessages(email)

    Debug "NumAttachedMessages after = " + Str(CkEmail::ckNumAttachedMessages(email))


    CkEmail::ckDispose(inner1)
    CkEmail::ckDispose(inner2)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure