Sample code for 30+ languages & platforms
AutoIt 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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oEmail = ObjCreate("Chilkat.Email")
$bSuccess = $oEmail.LoadEml("message.eml")
If ($bSuccess <> True) Then
    ConsoleWrite($oEmail.LastErrorText & @CRLF)
    Exit
EndIf

$oFields = ObjCreate("Chilkat.Xml")
$oFields.Tag = "DeliveryStatusFields"
$oFields.NewChild2 "Final-Recipient","rfc822; alex@example.net"
$oFields.NewChild2 "Action","failed"
$oFields.NewChild2 "Status","5.1.1"
$oFields.NewChild2 "Diagnostic-Code","smtp; 550 5.1.1 Mailbox does not exist"
$oReport = ObjCreate("Chilkat.Email")
$bSuccess = $oEmail.ToDsn("The destination mailbox does not exist.",$oFields.GetXml(),True,$oReport)
If ($bSuccess <> True) Then
    ConsoleWrite($oEmail.LastErrorText & @CRLF)
    Exit
EndIf

$oReport.From = "Mail delivery agent <postmaster@example.net>"
;  RFC 3464 puts the reporting MTA in its own block, ahead of the recipient fields.
$oMimeBytes = ObjCreate("Chilkat.BinData")
$bSuccess = $oReport.GetMimeBd($oMimeBytes)
If ($bSuccess <> True) Then
    ConsoleWrite($oReport.LastErrorText & @CRLF)
    Exit
EndIf

$oMime = ObjCreate("Chilkat.Mime")
$bSuccess = $oMime.LoadMimeBd($oMimeBytes)
If ($bSuccess <> True) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

$oStatusPart = ObjCreate("Chilkat.Mime")
$bSuccess = $oMime.PartAt(1,$oStatusPart)
If ($bSuccess <> True) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

$oStatus = ObjCreate("Chilkat.StringBuilder")
$oStatus.Append("Reporting-MTA: dns; mx.example.net" & @CRLF & @CRLF)
$oStatus.Append($oStatusPart.GetBodyDecoded())
$oStatusPart.SetBody $oStatus.GetAsString()
$bSuccess = $oMime.SaveMime("delivery-status.eml")
If ($bSuccess <> True) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Saved delivery-status.eml." & @CRLF)