Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 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

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckLoadEml(email,"qa_data/eml/sample.eml")
    If success = 0
        Debug "Failed to load .eml"
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    sbContentType.i = CkStringBuilder::ckCreate()
    If sbContentType.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    caseSensitive.i = 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)
    inlineOnly.i = 0
    excludeAttachments.i = 0
    searchSpec.s = "*/*"

    numParts.i = CkEmail::ckGetNumPartsOfType(email,searchSpec,inlineOnly,excludeAttachments)
    i.i = 0
    While i < numParts
        ; What is the Content-Type of this MIME part?
        CkStringBuilder::ckAppend(sbContentType,CkEmail::ckGetNthContentType(email,i,searchSpec,inlineOnly,excludeAttachments))
        If CkStringBuilder::ckStartsWith(sbContentType,"text/",caseSensitive) = 1
            ; Get the text body of this MIME part.
            textBody.s = CkEmail::ckGetNthTextPartOfType(email,i,searchSpec,inlineOnly,excludeAttachments)
            Debug "Got text body for " + CkStringBuilder::ckGetAsString(sbContentType)
        Else
            If CkStringBuilder::ckContentsEqual(sbContentType,"message/rfc822",caseSensitive) = 1
                ; 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...
                attachedEmail.i = CkEmail::ckCreate()
                If attachedEmail.i = 0
                    Debug "Failed to create object."
                    ProcedureReturn
                EndIf

                bdMime.i = CkBinData::ckCreate()
                If bdMime.i = 0
                    Debug "Failed to create object."
                    ProcedureReturn
                EndIf

                CkEmail::ckGetNthBinaryPartOfTypeBd(email,i,searchSpec,inlineOnly,excludeAttachments,bdMime)
                CkEmail::ckSetFromMimeBd(attachedEmail,bdMime)
                ; Now your app can recursively process the attachedEmail...
            Else
                ; Get the bytes of this MIME body part.
                bd.i = CkBinData::ckCreate()
                If bd.i = 0
                    Debug "Failed to create object."
                    ProcedureReturn
                EndIf

                CkEmail::ckGetNthBinaryPartOfTypeBd(email,i,searchSpec,inlineOnly,excludeAttachments,bd)
                Debug "Got binary body for " + CkStringBuilder::ckGetAsString(sbContentType) + " numBytes = " + Str(CkBinData::ckNumBytes(bd))
            EndIf

        EndIf

        CkStringBuilder::ckClear(sbContentType)
        i = i + 1
    Wend


    CkEmail::ckDispose(email)
    CkStringBuilder::ckDispose(sbContentType)
    CkEmail::ckDispose(attachedEmail)
    CkBinData::ckDispose(bdMime)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure