Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

Create a delivery failure report

See more Email Object Examples

A delivery agent can report a failure for an actual envelope recipient. Supply its observed SMTP outcome and enhanced status code, and return only the original headers when that is sufficient. This example saves a report for inspection. DSN format.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Handle hoFields
    Variant vReport
    Handle hoReport
    Variant vMimeBytes
    Handle hoMimeBytes
    Handle hoMime
    Variant vStatusPart
    Handle hoStatusPart
    Handle hoStatus
    String sTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get ComLoadEml Of hoEmail "message.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoFields
    If (Not(IsComObjectCreated(hoFields))) Begin
        Send CreateComObject of hoFields
    End
    Set ComTag Of hoFields To "DeliveryStatusFields"
    Send ComNewChild2 To hoFields "Final-Recipient" "rfc822; alex@example.net"
    Send ComNewChild2 To hoFields "Action" "failed"
    Send ComNewChild2 To hoFields "Status" "5.1.1"
    Send ComNewChild2 To hoFields "Diagnostic-Code" "smtp; 550 5.1.1 Mailbox does not exist"
    Get Create (RefClass(cComChilkatEmail)) To hoReport
    If (Not(IsComObjectCreated(hoReport))) Begin
        Send CreateComObject of hoReport
    End
    Get ComGetXml Of hoFields To sTemp1
    Get pvComObject of hoReport to vReport
    Get ComToDsn Of hoEmail "The destination mailbox does not exist." sTemp1 True vReport To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComFrom Of hoReport To "Mail delivery agent <postmaster@example.net>"
    //  RFC 3464 puts the reporting MTA in its own block, ahead of the recipient fields.
    Get Create (RefClass(cComChilkatBinData)) To hoMimeBytes
    If (Not(IsComObjectCreated(hoMimeBytes))) Begin
        Send CreateComObject of hoMimeBytes
    End
    Get pvComObject of hoMimeBytes to vMimeBytes
    Get ComGetMimeBd Of hoReport vMimeBytes To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoReport To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get pvComObject of hoMimeBytes to vMimeBytes
    Get ComLoadMimeBd Of hoMime vMimeBytes To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatMime)) To hoStatusPart
    If (Not(IsComObjectCreated(hoStatusPart))) Begin
        Send CreateComObject of hoStatusPart
    End
    Get pvComObject of hoStatusPart to vStatusPart
    Get ComPartAt Of hoMime 1 vStatusPart To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoStatus
    If (Not(IsComObjectCreated(hoStatus))) Begin
        Send CreateComObject of hoStatus
    End
    Get ComAppend Of hoStatus "Reporting-MTA: dns; mx.example.net" + (character(13)) + (character(10)) + (character(13)) + (character(10)) To iSuccess
    Get ComGetBodyDecoded Of hoStatusPart To sTemp1
    Get ComAppend Of hoStatus sTemp1 To iSuccess
    Get ComGetAsString Of hoStatus To sTemp1
    Send ComSetBody To hoStatusPart sTemp1
    Get ComSaveMime Of hoMime "delivery-status.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Saved delivery-status.eml."


End_Procedure