Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Handle hoXml
    Handle hoDtNow
    Boolean iHeaderOnly
    Handle hoSbText
    String sExplain
    Variant vDsnEmail
    Handle hoDsnEmail
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    Set ComSubject Of hoEmail To "Test"
    Set ComFrom Of hoEmail To "joe@example.com"
    Set ComBody Of hoEmail To "This is a test"
    Get ComAddTo Of hoEmail "" "recipient@example.com" To iSuccess

    Get ComGetMime Of hoEmail To sTemp1
    Showln sTemp1
    Showln ""
    Showln "-------------------------------------------------------------"
    Showln ""

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

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "DeliveryStatusFields"
    Send ComNewChild2 To hoXml "Final-Recipient" "rfc822; recipient@example.com"
    Send ComNewChild2 To hoXml "Action" "failed"
    Send ComNewChild2 To hoXml "Status" "5.1.2"
    Send ComNewChild2 To hoXml "Diagnostic-Code" "smtp; 550 5.1.2 Host unknown (Domain name not found)"
    Get Create (RefClass(cComCkDateTime)) To hoDtNow
    If (Not(IsComObjectCreated(hoDtNow))) Begin
        Send CreateComObject of hoDtNow
    End
    Get ComSetFromCurrentSystemTime Of hoDtNow To iSuccess
    Get ComGetAsRfc822 Of hoDtNow True To sTemp1
    Send ComNewChild2 To hoXml "Last-Attempt-Date" sTemp1

    Move True To iHeaderOnly
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbText
    If (Not(IsComObjectCreated(hoSbText))) Begin
        Send CreateComObject of hoSbText
    End
    Get ComAppend Of hoSbText "This is an automatically generated Delivery Status Notification." + (character(13)) + (character(10)) + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbText "Delivery to the following recipient failed permanently:" + (character(13)) + (character(10)) + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbText "    recipient@example.com" + (character(13)) + (character(10)) + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbText "Technical details of permanent failure:" + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbText "DNS Error: Domain name not found" + (character(13)) + (character(10)) To iSuccess

    Get ComGetAsString Of hoSbText To sExplain

    Get Create (RefClass(cComChilkatEmail)) To hoDsnEmail
    If (Not(IsComObjectCreated(hoDsnEmail))) Begin
        Send CreateComObject of hoDsnEmail
    End
    Get ComGetXml Of hoXml To sTemp1
    Get pvComObject of hoDsnEmail to vDsnEmail
    Get ComToDsn Of hoEmail sExplain sTemp1 iHeaderOnly vDsnEmail To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetMime Of hoDsnEmail To sTemp1
    Showln sTemp1

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


End_Procedure