Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
string ls_EmlPath
oleobject loo_Mime
oleobject loo_Email
integer i
integer li_NumAttach
integer li_NumRelated
oleobject loo_Em
integer li_NumAttachedMessages
integer li_NumReports
oleobject loo_JsonDsnInfo

li_Success = 0

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

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

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
if li_rc < 0 then
    destroy loo_Mime
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Success = loo_Mime.LoadMimeFile(ls_EmlPath)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

Write-Debug "---- MIME structure ----"
Write-Debug loo_Mime.GetStructure("text")
Write-Debug "------------------------"

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Email.LoadEml(ls_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.
Write-Debug "Email was Signed: " + string(loo_Email.ReceivedSigned)
Write-Debug "Email was Encrypted: " + string(loo_Email.ReceivedEncrypted)
if loo_Email.ReceivedSigned = 1 then
    Write-Debug "Signature(s) valid = " + string(loo_Email.SignaturesValid)
end if

if loo_Email.ReceivedEncrypted = 1 then
    Write-Debug "Decrypted successfully = " + string(loo_Email.Decrypted)
end if

i = 0
li_NumAttach = loo_Email.NumAttachments
Write-Debug "Number of attachments = " + string(li_NumAttach)

do while i < li_NumAttach
    Write-Debug "---- Attachment " + string(i)

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

    i = i + 1
loop
Write-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.

li_NumRelated = loo_Email.NumRelatedItems
Write-Debug "Number of related items = " + string(li_NumRelated)
i = 0
do while i < li_NumRelated
    Write-Debug "---- Related Item " + string(i)

    // Examine the filename (if any)
    Write-Debug "filename: " + loo_Email.GetRelatedFilename(i)
    // Examine the content-ID (if any)
    Write-Debug "Content-ID: " + loo_Email.GetRelatedContentID(i)
    // Examine the content-type
    Write-Debug "Content-Type: " + loo_Email.GetRelatedContentType(i)
    // Examine the content-location (if any)
    Write-Debug "Content-Location" + loo_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.
loo_Em = create oleobject
li_rc = loo_Em.ConnectToNewObject("Chilkat.Email")

li_NumAttachedMessages = loo_Email.NumAttachedMessages
Write-Debug "Number of attached messages = " + string(li_NumAttachedMessages)
i = 0
do while i < li_NumAttachedMessages
    Write-Debug "---- Attached message " + string(i)

    // Examine the attached email
    loo_Email.GetAttachedEmail(i,loo_Em)
    Write-Debug "from: " + loo_Em.From
    Write-Debug "subject: " + loo_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.
li_NumReports = loo_Email.NumReports
Write-Debug "Number of reports = " + string(li_NumReports)
i = 0
do while i < li_NumReports
    Write-Debug "---- Report " + string(i)
    // Get the raw report data...
    Write-Debug loo_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 loo_Email.IsMultipartReport() = 1 then

    Write-Debug "--- Delivery Status Information:"
    Write-Debug "Status: " + loo_Email.GetDeliveryStatusInfo("Status")
    Write-Debug "Action: " + loo_Email.GetDeliveryStatusInfo("Action")
    Write-Debug "Reporting-MTA: " + loo_Email.GetDeliveryStatusInfo("Reporting-MTA")

    loo_JsonDsnInfo = create oleobject
    li_rc = loo_JsonDsnInfo.ConnectToNewObject("Chilkat.JsonObject")

    loo_Email.GetDsnInfo(loo_JsonDsnInfo)
    loo_JsonDsnInfo.EmitCompact = 0
    Write-Debug loo_JsonDsnInfo.Emit()
end if



destroy loo_Mime
destroy loo_Email
destroy loo_Em
destroy loo_JsonDsnInfo