Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkXml.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkDateTime.pb"

Procedure ChilkatExample()

    success.i = 0

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Test")
    CkEmail::setCkFrom(email, "joe@example.com")
    CkEmail::setCkBody(email, "This is a test")
    CkEmail::ckAddTo(email,"","recipient@example.com")

    Debug CkEmail::ckGetMime(email)
    Debug ""
    Debug "-------------------------------------------------------------"
    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)

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "DeliveryStatusFields")
    CkXml::ckNewChild2(xml,"Final-Recipient","rfc822; recipient@example.com")
    CkXml::ckNewChild2(xml,"Action","failed")
    CkXml::ckNewChild2(xml,"Status","5.1.2")
    CkXml::ckNewChild2(xml,"Diagnostic-Code","smtp; 550 5.1.2 Host unknown (Domain name not found)")
    dtNow.i = CkDateTime::ckCreate()
    If dtNow.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckSetFromCurrentSystemTime(dtNow)
    CkXml::ckNewChild2(xml,"Last-Attempt-Date",CkDateTime::ckGetAsRfc822(dtNow,1))

    headerOnly.i = 1
    sbText.i = CkStringBuilder::ckCreate()
    If sbText.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbText,"This is an automatically generated Delivery Status Notification." + Chr(13) + Chr(10) + Chr(13) + Chr(10))
    CkStringBuilder::ckAppend(sbText,"Delivery to the following recipient failed permanently:" + Chr(13) + Chr(10) + Chr(13) + Chr(10))
    CkStringBuilder::ckAppend(sbText,"    recipient@example.com" + Chr(13) + Chr(10) + Chr(13) + Chr(10))
    CkStringBuilder::ckAppend(sbText,"Technical details of permanent failure:" + Chr(13) + Chr(10))
    CkStringBuilder::ckAppend(sbText,"DNS Error: Domain name not found" + Chr(13) + Chr(10))

    explain.s = CkStringBuilder::ckGetAsString(sbText)

    dsnEmail.i = CkEmail::ckCreate()
    If dsnEmail.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckToDsn(email,explain,CkXml::ckGetXml(xml),headerOnly,dsnEmail)
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkXml::ckDispose(xml)
        CkDateTime::ckDispose(dtNow)
        CkStringBuilder::ckDispose(sbText)
        CkEmail::ckDispose(dsnEmail)
        ProcedureReturn
    EndIf

    Debug CkEmail::ckGetMime(dsnEmail)

    ; 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--


    CkEmail::ckDispose(email)
    CkXml::ckDispose(xml)
    CkDateTime::ckDispose(dtNow)
    CkStringBuilder::ckDispose(sbText)
    CkEmail::ckDispose(dsnEmail)


    ProcedureReturn
EndProcedure