Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkEmailW.h>
#include <CkXmlW.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkEmailW email;

    email.put_Subject(L"Test");
    email.put_From(L"joe@example.com");
    email.put_Body(L"This is a test");
    email.AddTo(L"",L"recipient@example.com");

    wprintf(L"%s\n",email.getMime());
    wprintf(L"\n");
    wprintf(L"-------------------------------------------------------------\n");
    wprintf(L"\n");

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

    CkXmlW xml;
    xml.put_Tag(L"DeliveryStatusFields");
    xml.NewChild2(L"Final-Recipient",L"rfc822; recipient@example.com");
    xml.NewChild2(L"Action",L"failed");
    xml.NewChild2(L"Status",L"5.1.2");
    xml.NewChild2(L"Diagnostic-Code",L"smtp; 550 5.1.2 Host unknown (Domain name not found)");
    CkDateTimeW dtNow;
    dtNow.SetFromCurrentSystemTime();
    xml.NewChild2(L"Last-Attempt-Date",dtNow.getAsRfc822(true));

    bool headerOnly = true;
    CkStringBuilderW sbText;
    sbText.Append(L"This is an automatically generated Delivery Status Notification.\r\n\r\n");
    sbText.Append(L"Delivery to the following recipient failed permanently:\r\n\r\n");
    sbText.Append(L"    recipient@example.com\r\n\r\n");
    sbText.Append(L"Technical details of permanent failure:\r\n");
    sbText.Append(L"DNS Error: Domain name not found\r\n");

    const wchar_t *explain = sbText.getAsString();

    CkEmailW dsnEmail;
    success = email.ToDsn(explain,xml.getXml(),headerOnly,dsnEmail);
    if (success == false) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    wprintf(L"%s\n",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--
    }