Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkMime.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    emlPath.s = "C:/AAWorkarea/beatrix/roesner.eml"

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

    success = CkMime::ckLoadMimeFile(mime,emlPath)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    Debug "---- MIME structure ----"
    Debug CkMime::ckGetStructure(mime,"text")
    Debug "------------------------"

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

    success = CkEmail::ckLoadEml(email,emlPath)

    ; 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.
    Debug "Email was Signed: " + Str(CkEmail::ckReceivedSigned(email))
    Debug "Email was Encrypted: " + Str(CkEmail::ckReceivedEncrypted(email))
    If CkEmail::ckReceivedSigned(email) = 1
        Debug "Signature(s) valid = " + Str(CkEmail::ckSignaturesValid(email))
    EndIf

    If CkEmail::ckReceivedEncrypted(email) = 1
        Debug "Decrypted successfully = " + Str(CkEmail::ckDecrypted(email))
    EndIf

    i.i = 0
    numAttach.i = CkEmail::ckNumAttachments(email)
    Debug "Number of attachments = " + Str(numAttach)

    While i < numAttach
        Debug "---- Attachment " + Str(i)

        ; Examine the filename (if any)
        Debug "filename: " + CkEmail::ckGetAttachmentFilename(email,i)
        ; Examine the content-ID (if any)
        Debug "Content-ID: " + CkEmail::ckGetAttachmentContentID(email,i)
        ; Examine the content-type
        Debug "Content-Type: " + CkEmail::ckGetAttachmentContentType(email,i)
        ; Examine the content-disposition
        Debug "Content-Disposition" + CkEmail::ckGetAttachmentHeader(email,i,"content-disposition")
        ; Examine the attachment size:
        Debug "Size (in bytes) of the attachment: " + Str(CkEmail::ckGetAttachmentSize(email,i))

        i = i + 1
    Wend
    Debug "--"

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

    numRelated.i = CkEmail::ckNumRelatedItems(email)
    Debug "Number of related items = " + Str(numRelated)
    i = 0
    While i < numRelated
        Debug "---- Related Item " + Str(i)

        ; Examine the filename (if any)
        Debug "filename: " + CkEmail::ckGetRelatedFilename(email,i)
        ; Examine the content-ID (if any)
        Debug "Content-ID: " + CkEmail::ckGetRelatedContentID(email,i)
        ; Examine the content-type
        Debug "Content-Type: " + CkEmail::ckGetRelatedContentType(email,i)
        ; Examine the content-location (if any)
        Debug "Content-Location" + CkEmail::ckGetRelatedContentLocation(email,i)

        i = i + 1
    Wend

    ; The email could also have attached messages.
    ; An attached message is another email that was attached to this email.
    em.i = CkEmail::ckCreate()
    If em.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    numAttachedMessages.i = CkEmail::ckNumAttachedMessages(email)
    Debug "Number of attached messages = " + Str(numAttachedMessages)
    i = 0
    While i < numAttachedMessages
        Debug "---- Attached message " + Str(i)

        ; Examine the attached email
        CkEmail::ckGetAttachedEmail(email,i,em)
        Debug "from: " + CkEmail::ckFrom(em)
        Debug "subject: " + CkEmail::ckSubject(em)
        i = i + 1
    Wend

    ; An email could also be a multipart/report email. 
    ; This is a DSN (Delivery Status Notification)
    ; The NumReports property indicates how many "reports" exist.
    numReports.i = CkEmail::ckNumReports(email)
    Debug "Number of reports = " + Str(numReports)
    i = 0
    While i < numReports
        Debug "---- Report " + Str(i)
        ; Get the raw report data...
        Debug CkEmail::ckGetReport(email,i)
        i = i + 1
    Wend

    ; If the email is a multipart/report, then the information
    ; from the message/delivery-status part of the email can be retrieved:
    If CkEmail::ckIsMultipartReport(email) = 1

        Debug "--- Delivery Status Information:"
        Debug "Status: " + CkEmail::ckGetDeliveryStatusInfo(email,"Status")
        Debug "Action: " + CkEmail::ckGetDeliveryStatusInfo(email,"Action")
        Debug "Reporting-MTA: " + CkEmail::ckGetDeliveryStatusInfo(email,"Reporting-MTA")

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

        CkEmail::ckGetDsnInfo(email,jsonDsnInfo)
        CkJsonObject::setCkEmitCompact(jsonDsnInfo, 0)
        Debug CkJsonObject::ckEmit(jsonDsnInfo)
    EndIf



    CkMime::ckDispose(mime)
    CkEmail::ckDispose(email)
    CkEmail::ckDispose(em)
    CkJsonObject::ckDispose(jsonDsnInfo)


    ProcedureReturn
EndProcedure