Sample code for 30+ languages & platforms
DataFlex

Count the Attached Messages in an Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumAttachedMessages property, which is the number of embedded emails represented by message/rfc822 MIME parts. These are counted separately from ordinary attachments and related items, and their indexes are zero-based. This example builds an inner email, attaches it to an outer email as a nested message, and prints the count.

Background: When you "forward as attachment," many mail clients embed the original message as a complete nested email rather than quoting its text. In MIME this appears as a message/rfc822 part — an entire email (its own headers and body) tucked inside the carrier message. Chilkat distinguishes three kinds of enclosed content: ordinary attachments (NumAttachments), inline related items (NumRelatedItems), and these nested messages (NumAttachedMessages). Use GetAttachedEmail to pull an embedded message into its own Email object.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vInnerEmail
    Handle hoInnerEmail
    Handle hoEmail
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the read-only Email.NumAttachedMessages property, which is the
    //  number of embedded emails (message/rfc822 MIME parts) contained in the email.

    //  Create an inner email that we'll attach as a complete nested message.
    Get Create (RefClass(cComChilkatEmail)) To hoInnerEmail
    If (Not(IsComObjectCreated(hoInnerEmail))) Begin
        Send CreateComObject of hoInnerEmail
    End
    Set ComSubject Of hoInnerEmail To "I am an attached message"
    Set ComBody Of hoInnerEmail To "This entire email is nested inside another email."
    Set ComFrom Of hoInnerEmail To "alice@example.com"
    Get ComAddTo Of hoInnerEmail "Bob" "bob@example.com" To iSuccess

    //  Create the outer email and attach the inner email directly as a
    //  message/rfc822 part.  AttachEmail attaches a copy of the inner email.
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Outer email with an attached message"
    Set ComBody Of hoEmail To "See the attached email."
    Get pvComObject of hoInnerEmail to vInnerEmail
    Get ComAttachEmail Of hoEmail vInnerEmail To iSuccess

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



End_Procedure