Sample code for 30+ languages & platforms
B4X 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 B4X Downloads

B4X
Dim success As Boolean = False

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

Dim emlPath As String = "C:/AAWorkarea/beatrix/roesner.eml"

Dim mime As ChilkatMime
mime.Initialize

success = mime.LoadMimeFile(emlPath)
If success = False Then
    Log(mime.LastErrorText)
    Return
End If


Log("---- MIME structure ----")
Log(mime.GetStructure("text"))
Log("------------------------")

Dim email As ChilkatEmail
email.Initialize
success = email.LoadEml(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.
Log("Email was Signed: " & email.ReceivedSigned)
Log("Email was Encrypted: " & email.ReceivedEncrypted)
If email.ReceivedSigned = True Then
    Log("Signature(s) valid = " & email.SignaturesValid)
End If

If email.ReceivedEncrypted = True Then
    Log("Decrypted successfully = " & email.Decrypted)
End If


Dim i As Int = 0
Dim numAttach As Int = email.NumAttachments
Log("Number of attachments = " & numAttach)

Do While i < numAttach
    Log("---- Attachment " & i)

    '  Examine the filename (if any)
    Log("filename: " & email.GetAttachmentFilename(i))
    '  Examine the content-ID (if any)
    Log("Content-ID: " & email.GetAttachmentContentID(i))
    '  Examine the content-type
    Log("Content-Type: " & email.GetAttachmentContentType(i))
    '  Examine the content-disposition
    Log("Content-Disposition" & email.GetAttachmentHeader(i, "content-disposition"))
    '  Examine the attachment size:
    Log("Size (in bytes) of the attachment: " & email.GetAttachmentSize(i))

    i = i + 1
Loop
Log("--")

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

Dim numRelated As Int = email.NumRelatedItems
Log("Number of related items = " & numRelated)
i = 0
Do While i < numRelated
    Log("---- Related Item " & i)

    '  Examine the filename (if any)
    Log("filename: " & email.GetRelatedFilename(i))
    '  Examine the content-ID (if any)
    Log("Content-ID: " & email.GetRelatedContentID(i))
    '  Examine the content-type
    Log("Content-Type: " & email.GetRelatedContentType(i))
    '  Examine the content-location (if any)
    Log("Content-Location" & email.GetRelatedContentLocation(i))

    i = i + 1
Loop

'  The email could also have attached messages.
'  An attached message is another email that was attached to this email.
Dim em As ChilkatEmail
em.Initialize
Dim numAttachedMessages As Int = email.NumAttachedMessages
Log("Number of attached messages = " & numAttachedMessages)
i = 0
Do While i < numAttachedMessages
    Log("---- Attached message " & i)

    '  Examine the attached email
    email.GetAttachedEmail(i, em)
    Log("from: " & em.From)
    Log("subject: " & em.Subject)
    i = i + 1
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.
Dim numReports As Int = email.NumReports
Log("Number of reports = " & numReports)
i = 0
Do While i < numReports
    Log("---- Report " & i)
    '  Get the raw report data...
    Log(email.GetReport(i))
    i = i + 1
Loop

'  If the email is a multipart/report, then the information
'  from the message/delivery-status part of the email can be retrieved:
If email.IsMultipartReport = True Then

    Log("--- Delivery Status Information:")
    Log("Status: " & email.GetDeliveryStatusInfo("Status"))
    Log("Action: " & email.GetDeliveryStatusInfo("Action"))
    Log("Reporting-MTA: " & email.GetDeliveryStatusInfo("Reporting-MTA"))

    Dim jsonDsnInfo As ChilkatJsonObject
    jsonDsnInfo.Initialize
    email.GetDsnInfo(jsonDsnInfo)
    jsonDsnInfo.EmitCompact = False
    Log(jsonDsnInfo.Emit)
End If