Sample code for 30+ languages & platforms
PowerBuilder

Iterate MIME Parts of an Email

See more Email Object Examples

Demonstrates how to iterate over the MIME sub-parts of an email, and retrieve the content of each MIME sub-part body.

Note: This example requires some new features added to Chilkat v9.5.0.95.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_SbContentType
integer li_CaseSensitive
integer li_InlineOnly
integer li_ExcludeAttachments
string ls_SearchSpec
integer li_NumParts
integer i
string ls_TextBody
oleobject loo_AttachedEmail
oleobject loo_BdMime
oleobject loo_Bd

li_Success = 0

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

// See the following Chilkat post to Quickly Understand Email MIME

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

li_Success = loo_Email.LoadEml("qa_data/eml/sample.eml")
if li_Success = 0 then
    Write-Debug "Failed to load .eml"
    destroy loo_Email
    return
end if

loo_SbContentType = create oleobject
li_rc = loo_SbContentType.ConnectToNewObject("Chilkat.StringBuilder")

li_CaseSensitive = 0

// Get the total number of non-multipart MIME sub-parts.
// (This is a simple way of iterating over all the MIME leaf parts regardless of the MIME tree structure)
li_InlineOnly = 0
li_ExcludeAttachments = 0
ls_SearchSpec = "*/*"

li_NumParts = loo_Email.GetNumPartsOfType(ls_SearchSpec,li_InlineOnly,li_ExcludeAttachments)
i = 0
do while i < li_NumParts
    // What is the Content-Type of this MIME part?
    loo_SbContentType.Append(loo_Email.GetNthContentType(i,ls_SearchSpec,li_InlineOnly,li_ExcludeAttachments))
    if loo_SbContentType.StartsWith("text/",li_CaseSensitive) = 1 then
        // Get the text body of this MIME part.
        ls_TextBody = loo_Email.GetNthTextPartOfType(i,ls_SearchSpec,li_InlineOnly,li_ExcludeAttachments)
        Write-Debug "Got text body for " + loo_SbContentType.GetAsString()
    else
        if loo_SbContentType.ContentsEqual("message/rfc822",li_CaseSensitive) = 1 then
            // If the Content-Type is message/rfc822, then the MIME body for this part contains a full embedded MIME messages.
            // Your application could load it into a Chilkat email object and recursively process...
            loo_AttachedEmail = create oleobject
            li_rc = loo_AttachedEmail.ConnectToNewObject("Chilkat.Email")

            loo_BdMime = create oleobject
            li_rc = loo_BdMime.ConnectToNewObject("Chilkat.BinData")

            loo_Email.GetNthBinaryPartOfTypeBd(i,ls_SearchSpec,li_InlineOnly,li_ExcludeAttachments,loo_BdMime)
            loo_AttachedEmail.SetFromMimeBd(loo_BdMime)
            // Now your app can recursively process the attachedEmail...
        else
            // Get the bytes of this MIME body part.
            loo_Bd = create oleobject
            li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

            loo_Email.GetNthBinaryPartOfTypeBd(i,ls_SearchSpec,li_InlineOnly,li_ExcludeAttachments,loo_Bd)
            Write-Debug "Got binary body for " + loo_SbContentType.GetAsString() + " numBytes = " + string(loo_Bd.NumBytes)
        end if

    end if

    loo_SbContentType.Clear()
    i = i + 1
loop


destroy loo_Email
destroy loo_SbContentType
destroy loo_AttachedEmail
destroy loo_BdMime
destroy loo_Bd