Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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.
    innerEmail.i = CkEmail::ckCreate()
    If innerEmail.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(innerEmail, "Embedded message")
    CkEmail::setCkFrom(innerEmail, "alice@example.com")
    CkEmail::setCkBody(innerEmail, "This is the embedded message.")

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

    CkEmail::setCkSubject(email, "Has an attached message")
    success = CkEmail::ckAttachEmail(email,innerEmail)
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(innerEmail)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ;  Copy the first embedded message (index 0) into its own Email object.
    attached.i = CkEmail::ckCreate()
    If attached.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckGetAttachedEmail(email,0,attached)
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(innerEmail)
        CkEmail::ckDispose(email)
        CkEmail::ckDispose(attached)
        ProcedureReturn
    EndIf

    Debug "Attached email subject: " + CkEmail::ckSubject(attached)
    Debug "Attached email from: " + CkEmail::ckFrom(attached)


    CkEmail::ckDispose(innerEmail)
    CkEmail::ckDispose(email)
    CkEmail::ckDispose(attached)


    ProcedureReturn
EndProcedure