Sample code for 30+ languages & platforms
DataFlex

Get an Attached Message as an Email Object

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachedEmail method, which copies the Nth embedded message/rfc822 MIME part into another Email object. The index is zero-based (valid values run from 0 through NumAttachedMessages - 1). This example attaches an email and then extracts it back into its own object.

Background: When a message forwards another email as an attachment, that nested message is a complete email in its own right. GetAttachedEmail turns it back into a fully navigable Email object so you can read its subject, sender, body, and even its own attachments — essential for processing forwarded mail, or for tools that unpack reported spam/phishing to examine the original.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vInnerEmail
    Handle hoInnerEmail
    Handle hoEmail
    Variant vAttached
    Handle hoAttached
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the GetAttachedEmail method, which copies the Nth embedded message/rfc822
    //  MIME part into another Email object.  The index is zero-based.

    //  Build an inner email and attach it to an outer email.
    Get Create (RefClass(cComChilkatEmail)) To hoInnerEmail
    If (Not(IsComObjectCreated(hoInnerEmail))) Begin
        Send CreateComObject of hoInnerEmail
    End
    Set ComSubject Of hoInnerEmail To "Embedded message"
    Set ComFrom Of hoInnerEmail To "alice@example.com"
    Set ComBody Of hoInnerEmail To "This is the embedded message."

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Has an attached message"
    Get pvComObject of hoInnerEmail to vInnerEmail
    Get ComAttachEmail Of hoEmail vInnerEmail To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Copy the first embedded message (index 0) into its own Email object.
    Get Create (RefClass(cComChilkatEmail)) To hoAttached
    If (Not(IsComObjectCreated(hoAttached))) Begin
        Send CreateComObject of hoAttached
    End
    Get pvComObject of hoAttached to vAttached
    Get ComGetAttachedEmail Of hoEmail 0 vAttached To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSubject Of hoAttached To sTemp1
    Showln "Attached email subject: " sTemp1
    Get ComFrom Of hoAttached To sTemp1
    Showln "Attached email from: " sTemp1


End_Procedure