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

Visual FoxPro
LOCAL lnSuccess
LOCAL loEmail
LOCAL loFields
LOCAL loReport
LOCAL loMimeBytes
LOCAL loMime
LOCAL loStatusPart
LOCAL loStatus

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loEmail = CreateObject('Chilkat.Email')
lnSuccess = loEmail.LoadEml("message.eml")
IF (lnSuccess <> 1) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    CANCEL
ENDIF

loFields = CreateObject('Chilkat.Xml')
loFields.Tag = "DeliveryStatusFields"
loFields.NewChild2("Final-Recipient","rfc822; alex@example.net")
loFields.NewChild2("Action","failed")
loFields.NewChild2("Status","5.1.1")
loFields.NewChild2("Diagnostic-Code","smtp; 550 5.1.1 Mailbox does not exist")
loReport = CreateObject('Chilkat.Email')
lnSuccess = loEmail.ToDsn("The destination mailbox does not exist.",loFields.GetXml(),1,loReport)
IF (lnSuccess <> 1) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    RELEASE loFields
    RELEASE loReport
    CANCEL
ENDIF

loReport.From = "Mail delivery agent <postmaster@example.net>"
*  RFC 3464 puts the reporting MTA in its own block, ahead of the recipient fields.
loMimeBytes = CreateObject('Chilkat.BinData')
lnSuccess = loReport.GetMimeBd(loMimeBytes)
IF (lnSuccess <> 1) THEN
    ? loReport.LastErrorText
    RELEASE loEmail
    RELEASE loFields
    RELEASE loReport
    RELEASE loMimeBytes
    CANCEL
ENDIF

loMime = CreateObject('Chilkat.Mime')
lnSuccess = loMime.LoadMimeBd(loMimeBytes)
IF (lnSuccess <> 1) THEN
    ? loMime.LastErrorText
    RELEASE loEmail
    RELEASE loFields
    RELEASE loReport
    RELEASE loMimeBytes
    RELEASE loMime
    CANCEL
ENDIF

loStatusPart = CreateObject('Chilkat.Mime')
lnSuccess = loMime.PartAt(1,loStatusPart)
IF (lnSuccess <> 1) THEN
    ? loMime.LastErrorText
    RELEASE loEmail
    RELEASE loFields
    RELEASE loReport
    RELEASE loMimeBytes
    RELEASE loMime
    RELEASE loStatusPart
    CANCEL
ENDIF

loStatus = CreateObject('Chilkat.StringBuilder')
loStatus.Append("Reporting-MTA: dns; mx.example.net" + CHR(13) + CHR(10) + CHR(13) + CHR(10))
loStatus.Append(loStatusPart.GetBodyDecoded())
loStatusPart.SetBody(loStatus.GetAsString())
lnSuccess = loMime.SaveMime("delivery-status.eml")
IF (lnSuccess <> 1) THEN
    ? loMime.LastErrorText
    RELEASE loEmail
    RELEASE loFields
    RELEASE loReport
    RELEASE loMimeBytes
    RELEASE loMime
    RELEASE loStatusPart
    RELEASE loStatus
    CANCEL
ENDIF

? "Saved delivery-status.eml."

RELEASE loEmail
RELEASE loFields
RELEASE loReport
RELEASE loMimeBytes
RELEASE loMime
RELEASE loStatusPart
RELEASE loStatus