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

Create DSN (Delivery Status Notification) Email

See more Email Object Examples

Demonstrates how to create a DSN (Delivery Status Notification) Email having the format as defined in RFC 3464.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oEmail
LOCAL oXml
LOCAL oDtNow
LOCAL nHeaderOnly
LOCAL oSbText
LOCAL cExplain
LOCAL oDsnEmail

nSuccess := 0

oEmail := CreateObject("Chilkat.Email")

oEmail:Subject := "Test"
oEmail:From := "joe@example.com"
oEmail:Body := "This is a test"
oEmail:AddTo("", "recipient@example.com")

? oEmail:GetMime()
? ""
? "-------------------------------------------------------------"
? ""

//  This is the email we just created:

//  MIME-Version: 1.0
//  Date: Mon, 12 May 2025 11:13:15 -0500
//  Message-ID: <917FA49F75544EF51948B0A52F403B925B51073F@SLICE>
//  Content-Type: text/plain; charset=us-ascii; format=flowed
//  Content-Transfer-Encoding: 7bit
//  X-Priority: 3 (Normal)
//  Subject: Test
//  From: joe@example.com
//  To: recipient@example.com
//  
//  This is a test

//  -----------------------------------------------------------------
//  Convert the above email into a DSN (Delivery Status Notification)

oXml := CreateObject("Chilkat.Xml")
oXml:Tag := "DeliveryStatusFields"
oXml:NewChild2("Final-Recipient", "rfc822; recipient@example.com")
oXml:NewChild2("Action", "failed")
oXml:NewChild2("Status", "5.1.2")
oXml:NewChild2("Diagnostic-Code", "smtp; 550 5.1.2 Host unknown (Domain name not found)")
oDtNow := CreateObject("Chilkat.CkDateTime")
oDtNow:SetFromCurrentSystemTime()
oXml:NewChild2("Last-Attempt-Date", oDtNow:GetAsRfc822(1))

nHeaderOnly := 1
oSbText := CreateObject("Chilkat.StringBuilder")
oSbText:Append("This is an automatically generated Delivery Status Notification." + Chr(13) + Chr(10) + Chr(13) + Chr(10))
oSbText:Append("Delivery to the following recipient failed permanently:" + Chr(13) + Chr(10) + Chr(13) + Chr(10))
oSbText:Append("    recipient@example.com" + Chr(13) + Chr(10) + Chr(13) + Chr(10))
oSbText:Append("Technical details of permanent failure:" + Chr(13) + Chr(10))
oSbText:Append("DNS Error: Domain name not found" + Chr(13) + Chr(10))

cExplain := oSbText:GetAsString()

oDsnEmail := CreateObject("Chilkat.Email")
nSuccess := oEmail:ToDsn(cExplain, oXml:GetXml(), nHeaderOnly, oDsnEmail)
IF (nSuccess == 0)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oXml:destroy()
    oDtNow:destroy()
    oSbText:destroy()
    oDsnEmail:destroy()
    RETURN
ENDIF

? oDsnEmail:GetMime()

//  Here's the MIME of the DNS email we just created:
//  -------------------------------------------------

//  Content-Type: multipart/report; report-type="delivery-status"; boundary="------------060100020300020303000802"
//  
//  --------------060100020300020303000802
//  Content-Type: text/plain; charset=windows-1252; format=flowed
//  Content-Transfer-Encoding: 7bit
//  
//  This is an automatically generated Delivery Status Notification.
//  
//  Delivery to the following recipient failed permanently:
//  
//      recipient@example.com
//  
//  Technical details of permanent failure:
//  DNS Error: Domain name not found
//  
//  --------------060100020300020303000802
//  Content-Type: message/delivery-status
//  
//  Final-Recipient: rfc822; recipient@example.com
//  Action: failed
//  Status: 5.1.2
//  Diagnostic-Code: smtp; 550 5.1.2 Host unknown (Domain name not found)
//  Last-Attempt-Date: Mon, 12 May 2025 11:30:39 -0500
//  
//  --------------060100020300020303000802
//  Content-Type: text/rfc822-headers; charset=windows-1252
//  
//  MIME-Version: 1.0
//  Date: Mon, 12 May 2025 11:30:39 -0500
//  Message-ID: <B8E6875D582A78AE779FC0B46ACC8C858CEAF608@SLICE>
//  Content-Type: text/plain; charset=us-ascii; format=flowed
//  Content-Transfer-Encoding: 7bit
//  X-Priority: 3 (Normal)
//  Subject: Test
//  From: joe@example.com
//  To: recipient@example.com
//  --------------060100020300020303000802--

oEmail:destroy()
oXml:destroy()
oDtNow:destroy()
oSbText:destroy()
oDsnEmail:destroy()