Sample code for 30+ languages & platforms
PureBasic

Count the Report Parts in a multipart/report Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumReports property, which is the number of report parts in a multipart/report email. A part is counted as a report when its Content-Type is message/* (except message/rfc822) or text/rfc822-headers. Use GetReport to retrieve the body of each report part; indexes are zero-based. This example loads a report email and prints each report part.

Background: When a message can't be delivered, the mail system usually sends back a bounce known as a Delivery Status Notification (DSN). DSNs use the multipart/report structure, which bundles several parts: a human-readable explanation, a machine-readable message/delivery-status part describing exactly what happened (recipient, status code, failing server), and often the original message's headers. Reading these report parts lets a program automatically detect and process bounces.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the read-only Email.NumReports property, which is the number of report
    ;  parts in a multipart/report email (for example, a Delivery Status Notification).
    ;  Use GetReport to retrieve the body of each report part.  Indexes are zero-based.

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

    ;  Load a multipart/report email (such as a bounce / DSN message).
    success = CkEmail::ckLoadEml(email,"qa_data/eml/dsn_bounce.eml")
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    n.i = CkEmail::ckNumReports(email)
    Debug "NumReports = " + Str(n)

    i.i
    For i = 0 To n - 1
        Debug "---- Report " + Str(i) + " ----"
        Debug CkEmail::ckGetReport(email,i)
    Next


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure