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 <C_CkEmailW.h>
#include <C_CkXmlW.h>
#include <C_CkDateTimeW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;
    HCkXmlW xml;
    HCkDateTimeW dtNow;
    BOOL headerOnly;
    HCkStringBuilderW sbText;
    const wchar_t *explain;
    HCkEmailW dsnEmail;

    success = FALSE;

    email = CkEmailW_Create();

    CkEmailW_putSubject(email,L"Test");
    CkEmailW_putFrom(email,L"joe@example.com");
    CkEmailW_putBody(email,L"This is a test");
    CkEmailW_AddTo(email,L"",L"recipient@example.com");

    wprintf(L"%s\n",CkEmailW_getMime(email));
    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)

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"DeliveryStatusFields");
    CkXmlW_NewChild2(xml,L"Final-Recipient",L"rfc822; recipient@example.com");
    CkXmlW_NewChild2(xml,L"Action",L"failed");
    CkXmlW_NewChild2(xml,L"Status",L"5.1.2");
    CkXmlW_NewChild2(xml,L"Diagnostic-Code",L"smtp; 550 5.1.2 Host unknown (Domain name not found)");
    dtNow = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dtNow);
    CkXmlW_NewChild2(xml,L"Last-Attempt-Date",CkDateTimeW_getAsRfc822(dtNow,TRUE));

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

    explain = CkStringBuilderW_getAsString(sbText);

    dsnEmail = CkEmailW_Create();
    success = CkEmailW_ToDsn(email,explain,CkXmlW_getXml(xml),headerOnly,dsnEmail);
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkXmlW_Dispose(xml);
        CkDateTimeW_Dispose(dtNow);
        CkStringBuilderW_Dispose(sbText);
        CkEmailW_Dispose(dsnEmail);
        return;
    }

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


    CkEmailW_Dispose(email);
    CkXmlW_Dispose(xml);
    CkDateTimeW_Dispose(dtNow);
    CkStringBuilderW_Dispose(sbText);
    CkEmailW_Dispose(dsnEmail);

    }