Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_Xml
oleobject loo_DtNow
integer li_HeaderOnly
oleobject loo_SbText
string ls_Explain
oleobject loo_DsnEmail

li_Success = 0

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

Write-Debug loo_Email.GetMime()
Write-Debug ""
Write-Debug "-------------------------------------------------------------"
Write-Debug ""

// 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)

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Tag = "DeliveryStatusFields"
loo_Xml.NewChild2("Final-Recipient","rfc822; recipient@example.com")
loo_Xml.NewChild2("Action","failed")
loo_Xml.NewChild2("Status","5.1.2")
loo_Xml.NewChild2("Diagnostic-Code","smtp; 550 5.1.2 Host unknown (Domain name not found)")
loo_DtNow = create oleobject
li_rc = loo_DtNow.ConnectToNewObject("Chilkat.CkDateTime")

loo_DtNow.SetFromCurrentSystemTime()
loo_Xml.NewChild2("Last-Attempt-Date",loo_DtNow.GetAsRfc822(1))

li_HeaderOnly = 1
loo_SbText = create oleobject
li_rc = loo_SbText.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbText.Append("This is an automatically generated Delivery Status Notification.~r~n~r~n")
loo_SbText.Append("Delivery to the following recipient failed permanently:~r~n~r~n")
loo_SbText.Append("    recipient@example.com~r~n~r~n")
loo_SbText.Append("Technical details of permanent failure:~r~n")
loo_SbText.Append("DNS Error: Domain name not found~r~n")

ls_Explain = loo_SbText.GetAsString()

loo_DsnEmail = create oleobject
li_rc = loo_DsnEmail.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Email.ToDsn(ls_Explain,loo_Xml.GetXml(),li_HeaderOnly,loo_DsnEmail)
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Xml
    destroy loo_DtNow
    destroy loo_SbText
    destroy loo_DsnEmail
    return
end if

Write-Debug loo_DsnEmail.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--


destroy loo_Email
destroy loo_Xml
destroy loo_DtNow
destroy loo_SbText
destroy loo_DsnEmail