DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoEmail
Handle hoSbContentType
Boolean iCaseSensitive
Boolean iInlineOnly
Boolean iExcludeAttachments
String sSearchSpec
Integer iNumParts
Integer i
String sTextBody
Handle hoAttachedEmail
Variant vBdMime
Handle hoBdMime
Variant vBd
Handle hoBd
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Get ComLoadEml Of hoEmail "qa_data/eml/sample.eml" To iSuccess
If (iSuccess = False) Begin
Showln "Failed to load .eml"
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContentType
If (Not(IsComObjectCreated(hoSbContentType))) Begin
Send CreateComObject of hoSbContentType
End
Move False To iCaseSensitive
// 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)
Move False To iInlineOnly
Move False To iExcludeAttachments
Move "*/*" To sSearchSpec
Get ComGetNumPartsOfType Of hoEmail sSearchSpec iInlineOnly iExcludeAttachments To iNumParts
Move 0 To i
While (i < iNumParts)
// What is the Content-Type of this MIME part?
Get ComGetNthContentType Of hoEmail i sSearchSpec iInlineOnly iExcludeAttachments To sTemp1
Get ComAppend Of hoSbContentType sTemp1 To iSuccess
Get ComStartsWith Of hoSbContentType "text/" iCaseSensitive To bTemp1
If (bTemp1 = True) Begin
// Get the text body of this MIME part.
Get ComGetNthTextPartOfType Of hoEmail i sSearchSpec iInlineOnly iExcludeAttachments To sTextBody
Get ComGetAsString Of hoSbContentType To sTemp1
Showln "Got text body for " sTemp1
End
Else Begin
Get ComContentsEqual Of hoSbContentType "message/rfc822" iCaseSensitive To bTemp1
If (bTemp1 = True) Begin
// 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...
Get Create (RefClass(cComChilkatEmail)) To hoAttachedEmail
If (Not(IsComObjectCreated(hoAttachedEmail))) Begin
Send CreateComObject of hoAttachedEmail
End
Get Create (RefClass(cComChilkatBinData)) To hoBdMime
If (Not(IsComObjectCreated(hoBdMime))) Begin
Send CreateComObject of hoBdMime
End
Get pvComObject of hoBdMime to vBdMime
Get ComGetNthBinaryPartOfTypeBd Of hoEmail i sSearchSpec iInlineOnly iExcludeAttachments vBdMime To iSuccess
Get pvComObject of hoBdMime to vBdMime
Get ComSetFromMimeBd Of hoAttachedEmail vBdMime To iSuccess
// Now your app can recursively process the attachedEmail...
End
Else Begin
// Get the bytes of this MIME body part.
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Get pvComObject of hoBd to vBd
Get ComGetNthBinaryPartOfTypeBd Of hoEmail i sSearchSpec iInlineOnly iExcludeAttachments vBd To iSuccess
Get ComGetAsString Of hoSbContentType To sTemp1
Get ComNumBytes Of hoBd To iTemp1
Showln "Got binary body for " sTemp1 " numBytes = " iTemp1
End
End
Send ComClear To hoSbContentType
Move (i + 1) To i
Loop
End_Procedure