Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cEmlPath
LOCAL oMime
LOCAL oEmail
LOCAL i
LOCAL nNumAttach
LOCAL nNumRelated
LOCAL oEm
LOCAL nNumAttachedMessages
LOCAL nNumReports
LOCAL oJsonDsnInfo

nSuccess := 0

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

cEmlPath := "C:/AAWorkarea/beatrix/roesner.eml"

oMime := CreateObject("Chilkat.Mime")

nSuccess := oMime:LoadMimeFile(cEmlPath)
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    RETURN
ENDIF

? "---- MIME structure ----"
? oMime:GetStructure("text")
? "------------------------"

oEmail := CreateObject("Chilkat.Email")
nSuccess := oEmail:LoadEml(cEmlPath)

//  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.
? "Email was Signed: " + Str(oEmail:ReceivedSigned)
? "Email was Encrypted: " + Str(oEmail:ReceivedEncrypted)
IF (oEmail:ReceivedSigned == 1)
    ? "Signature(s) valid = " + Str(oEmail:SignaturesValid)
ENDIF

IF (oEmail:ReceivedEncrypted == 1)
    ? "Decrypted successfully = " + Str(oEmail:Decrypted)
ENDIF

i := 0
nNumAttach := oEmail:NumAttachments
? "Number of attachments = " + Str(nNumAttach)

DO WHILE i < nNumAttach
    ? "---- Attachment " + Str(i)

    //  Examine the filename (if any)
    ? "filename: " + oEmail:GetAttachmentFilename(i)
    //  Examine the content-ID (if any)
    ? "Content-ID: " + oEmail:GetAttachmentContentID(i)
    //  Examine the content-type
    ? "Content-Type: " + oEmail:GetAttachmentContentType(i)
    //  Examine the content-disposition
    ? "Content-Disposition" + oEmail:GetAttachmentHeader(i, "content-disposition")
    //  Examine the attachment size:
    ? "Size (in bytes) of the attachment: " + Str(oEmail:GetAttachmentSize(i))

    i := i + 1
ENDDO
? "--"

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

nNumRelated := oEmail:NumRelatedItems
? "Number of related items = " + Str(nNumRelated)
i := 0
DO WHILE i < nNumRelated
    ? "---- Related Item " + Str(i)

    //  Examine the filename (if any)
    ? "filename: " + oEmail:GetRelatedFilename(i)
    //  Examine the content-ID (if any)
    ? "Content-ID: " + oEmail:GetRelatedContentID(i)
    //  Examine the content-type
    ? "Content-Type: " + oEmail:GetRelatedContentType(i)
    //  Examine the content-location (if any)
    ? "Content-Location" + oEmail:GetRelatedContentLocation(i)

    i := i + 1
ENDDO

//  The email could also have attached messages.
//  An attached message is another email that was attached to this email.
oEm := CreateObject("Chilkat.Email")
nNumAttachedMessages := oEmail:NumAttachedMessages
? "Number of attached messages = " + Str(nNumAttachedMessages)
i := 0
DO WHILE i < nNumAttachedMessages
    ? "---- Attached message " + Str(i)

    //  Examine the attached email
    oEmail:GetAttachedEmail(i, oEm)
    ? "from: " + oEm:From
    ? "subject: " + oEm:Subject
    i := i + 1
ENDDO

//  An email could also be a multipart/report email. 
//  This is a DSN (Delivery Status Notification)
//  The NumReports property indicates how many "reports" exist.
nNumReports := oEmail:NumReports
? "Number of reports = " + Str(nNumReports)
i := 0
DO WHILE i < nNumReports
    ? "---- Report " + Str(i)
    //  Get the raw report data...
    ? oEmail:GetReport(i)
    i := i + 1
ENDDO

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

    ? "--- Delivery Status Information:"
    ? "Status: " + oEmail:GetDeliveryStatusInfo("Status")
    ? "Action: " + oEmail:GetDeliveryStatusInfo("Action")
    ? "Reporting-MTA: " + oEmail:GetDeliveryStatusInfo("Reporting-MTA")

    oJsonDsnInfo := CreateObject("Chilkat.JsonObject")
    oEmail:GetDsnInfo(oJsonDsnInfo)
    oJsonDsnInfo:EmitCompact := 0
    ? oJsonDsnInfo:Emit()
ENDIF

oMime:destroy()
oEmail:destroy()
oEm:destroy()
oJsonDsnInfo:destroy()