Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcEmlPath
LOCAL loMime
LOCAL loEmail
LOCAL i
LOCAL lnNumAttach
LOCAL lnNumRelated
LOCAL loEm
LOCAL lnNumAttachedMessages
LOCAL lnNumReports
LOCAL loJsonDsnInfo

lnSuccess = 0

* 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('Chilkat.Mime')

lnSuccess = loMime.LoadMimeFile(lcEmlPath)
IF (lnSuccess = 0) THEN
    ? loMime.LastErrorText
    RELEASE loMime
    CANCEL
ENDIF

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

loEmail = CreateObject('Chilkat.Email')
lnSuccess = 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 = 1) THEN
    ? "Signature(s) valid = " + STR(loEmail.SignaturesValid)
ENDIF

IF (loEmail.ReceivedEncrypted = 1) 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('Chilkat.Email')
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() = 1) THEN

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

    loJsonDsnInfo = CreateObject('Chilkat.JsonObject')
    loEmail.GetDsnInfo(loJsonDsnInfo)
    loJsonDsnInfo.EmitCompact = 0
    ? loJsonDsnInfo.Emit()
ENDIF

RELEASE loMime
RELEASE loEmail
RELEASE loEm
RELEASE loJsonDsnInfo