Sample code for 30+ languages & platforms
DataFlex

Load .eml and Examine the Structure, Attachments, and Related Items

See more Email Object Examples

Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sEmlPath
    Handle hoMime
    Handle hoEmail
    Integer i
    Integer iNumAttach
    Integer iNumRelated
    Variant vEm
    Handle hoEm
    Integer iNumAttachedMessages
    Integer iNumReports
    Variant vJsonDsnInfo
    Handle hoJsonDsnInfo
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Move "C:/AAWorkarea/beatrix/roesner.eml" To sEmlPath

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End

    Get ComLoadMimeFile Of hoMime sEmlPath To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "---- MIME structure ----"
    Get ComGetStructure Of hoMime "text" To sTemp1
    Showln sTemp1
    Showln "------------------------"

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get ComLoadEml Of hoEmail sEmlPath To iSuccess

    // Was this a signed and/or encrypted email?
    // If so, then loading the .eml automatically unwraps
    // (i.e. verifies signatures and decrypts) and the resultant
    // email is what existed prior to signing/encrypting.
    Get ComReceivedSigned Of hoEmail To bTemp1
    Showln "Email was Signed: " bTemp1
    Get ComReceivedEncrypted Of hoEmail To bTemp1
    Showln "Email was Encrypted: " bTemp1
    Get ComReceivedSigned Of hoEmail To bTemp1
    If (bTemp1 = True) Begin
        Get ComSignaturesValid Of hoEmail To bTemp1
        Showln "Signature(s) valid = " bTemp1
    End

    Get ComReceivedEncrypted Of hoEmail To bTemp1
    If (bTemp1 = True) Begin
        Get ComDecrypted Of hoEmail To bTemp1
        Showln "Decrypted successfully = " bTemp1
    End

    Move 0 To i
    Get ComNumAttachments Of hoEmail To iNumAttach
    Showln "Number of attachments = " iNumAttach

    While (i < iNumAttach)
        Showln "---- Attachment " i

        // Examine the filename (if any)
        Get ComGetAttachmentFilename Of hoEmail i To sTemp1
        Showln "filename: " sTemp1
        // Examine the content-ID (if any)
        Get ComGetAttachmentContentID Of hoEmail i To sTemp1
        Showln "Content-ID: " sTemp1
        // Examine the content-type
        Get ComGetAttachmentContentType Of hoEmail i To sTemp1
        Showln "Content-Type: " sTemp1
        // Examine the content-disposition
        Get ComGetAttachmentHeader Of hoEmail i "content-disposition" To sTemp1
        Showln "Content-Disposition" sTemp1
        // Examine the attachment size:
        Get ComGetAttachmentSize Of hoEmail i To iTemp1
        Showln "Size (in bytes) of the attachment: " iTemp1

        Move (i + 1) To i
    Loop

    Showln "--"

    // Now for the related items.

    // Note: A MIME sub-part can potentially be both a related item AND an attachment.
    // The typical case is when the item is contained under the multipart/related enclosure and 
    // the item also has a "Content-Disposition" header indicating "attachment".
    // The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
    // Related items and attachments are not necessarily mutually exclusive.

    Get ComNumRelatedItems Of hoEmail To iNumRelated
    Showln "Number of related items = " iNumRelated
    Move 0 To i
    While (i < iNumRelated)
        Showln "---- Related Item " i

        // Examine the filename (if any)
        Get ComGetRelatedFilename Of hoEmail i To sTemp1
        Showln "filename: " sTemp1
        // Examine the content-ID (if any)
        Get ComGetRelatedContentID Of hoEmail i To sTemp1
        Showln "Content-ID: " sTemp1
        // Examine the content-type
        Get ComGetRelatedContentType Of hoEmail i To sTemp1
        Showln "Content-Type: " sTemp1
        // Examine the content-location (if any)
        Get ComGetRelatedContentLocation Of hoEmail i To sTemp1
        Showln "Content-Location" sTemp1

        Move (i + 1) To i
    Loop

    // The email could also have attached messages.
    // An attached message is another email that was attached to this email.
    Get Create (RefClass(cComChilkatEmail)) To hoEm
    If (Not(IsComObjectCreated(hoEm))) Begin
        Send CreateComObject of hoEm
    End
    Get ComNumAttachedMessages Of hoEmail To iNumAttachedMessages
    Showln "Number of attached messages = " iNumAttachedMessages
    Move 0 To i
    While (i < iNumAttachedMessages)
        Showln "---- Attached message " i

        // Examine the attached email
        Get pvComObject of hoEm to vEm
        Get ComGetAttachedEmail Of hoEmail i vEm To iSuccess
        Get ComFrom Of hoEm To sTemp1
        Showln "from: " sTemp1
        Get ComSubject Of hoEm To sTemp1
        Showln "subject: " sTemp1
        Move (i + 1) To i
    Loop

    // An email could also be a multipart/report email. 
    // This is a DSN (Delivery Status Notification)
    // The NumReports property indicates how many "reports" exist.
    Get ComNumReports Of hoEmail To iNumReports
    Showln "Number of reports = " iNumReports
    Move 0 To i
    While (i < iNumReports)
        Showln "---- Report " i
        // Get the raw report data...
        Get ComGetReport Of hoEmail i To sTemp1
        Showln sTemp1
        Move (i + 1) To i
    Loop

    // If the email is a multipart/report, then the information
    // from the message/delivery-status part of the email can be retrieved:
    Get ComIsMultipartReport Of hoEmail To bTemp1
    If (bTemp1 = True) Begin

        Showln "--- Delivery Status Information:"
        Get ComGetDeliveryStatusInfo Of hoEmail "Status" To sTemp1
        Showln "Status: " sTemp1
        Get ComGetDeliveryStatusInfo Of hoEmail "Action" To sTemp1
        Showln "Action: " sTemp1
        Get ComGetDeliveryStatusInfo Of hoEmail "Reporting-MTA" To sTemp1
        Showln "Reporting-MTA: " sTemp1

        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonDsnInfo
        If (Not(IsComObjectCreated(hoJsonDsnInfo))) Begin
            Send CreateComObject of hoJsonDsnInfo
        End
        Get pvComObject of hoJsonDsnInfo to vJsonDsnInfo
        Get ComGetDsnInfo Of hoEmail vJsonDsnInfo To iSuccess
        Set ComEmitCompact Of hoJsonDsnInfo To False
        Get ComEmit Of hoJsonDsnInfo To sTemp1
        Showln sTemp1
    End



End_Procedure