Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

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

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

loMime = createobject("CkMime")

llSuccess = loMime.LoadMimeFile(lcEmlPath)
if (llSuccess = .F.) then
    ? loMime.LastErrorText
    release loMime
    return
endif

? "---- MIME structure ----"
? loMime.GetStructure("text")
? "------------------------"

loEmail = createobject("CkEmail")
llSuccess = loEmail.LoadEml(lcEmlPath)

// 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(loEmail.ReceivedSigned)
? "Email was Encrypted: " + str(loEmail.ReceivedEncrypted)
if (loEmail.ReceivedSigned = .T.) then
    ? "Signature(s) valid = " + str(loEmail.SignaturesValid)
endif

if (loEmail.ReceivedEncrypted = .T.) then
    ? "Decrypted successfully = " + str(loEmail.Decrypted)
endif

i = 0
lnNumAttach = loEmail.NumAttachments
? "Number of attachments = " + str(lnNumAttach)

do while i < lnNumAttach
    ? "---- Attachment " + str(i)

    // Examine the filename (if any)
    ? "filename: " + loEmail.GetAttachmentFilename(i)
    // Examine the content-ID (if any)
    ? "Content-ID: " + loEmail.GetAttachmentContentID(i)
    // Examine the content-type
    ? "Content-Type: " + loEmail.GetAttachmentContentType(i)
    // Examine the content-disposition
    ? "Content-Disposition" + loEmail.GetAttachmentHeader(i,"content-disposition")
    // Examine the attachment size:
    ? "Size (in bytes) of the attachment: " + str(loEmail.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.

lnNumRelated = loEmail.NumRelatedItems
? "Number of related items = " + str(lnNumRelated)
i = 0
do while i < lnNumRelated
    ? "---- Related Item " + str(i)

    // Examine the filename (if any)
    ? "filename: " + loEmail.GetRelatedFilename(i)
    // Examine the content-ID (if any)
    ? "Content-ID: " + loEmail.GetRelatedContentID(i)
    // Examine the content-type
    ? "Content-Type: " + loEmail.GetRelatedContentType(i)
    // Examine the content-location (if any)
    ? "Content-Location" + loEmail.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.
loEm = createobject("CkEmail")
lnNumAttachedMessages = loEmail.NumAttachedMessages
? "Number of attached messages = " + str(lnNumAttachedMessages)
i = 0
do while i < lnNumAttachedMessages
    ? "---- Attached message " + str(i)

    // Examine the attached email
    loEmail.GetAttachedEmail(i,loEm)
    ? "from: " + loEm.From
    ? "subject: " + loEm.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.
lnNumReports = loEmail.NumReports
? "Number of reports = " + str(lnNumReports)
i = 0
do while i < lnNumReports
    ? "---- Report " + str(i)
    // Get the raw report data...
    ? loEmail.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 (loEmail.IsMultipartReport() = .T.) then

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

    loJsonDsnInfo = createobject("CkJsonObject")
    loEmail.GetDsnInfo(loJsonDsnInfo)
    loJsonDsnInfo.EmitCompact = .F.
    ? loJsonDsnInfo.Emit()
endif



release loMime
release loEmail
release loEm
release loJsonDsnInfo