Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkEmail.h>
#include <C_CkXml.h>
#include <C_CkDateTime.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;
    HCkXml xml;
    HCkDateTime dtNow;
    BOOL headerOnly;
    HCkStringBuilder sbText;
    const char *explain;
    HCkEmail dsnEmail;

    success = FALSE;

    email = CkEmail_Create();

    CkEmail_putSubject(email,"Test");
    CkEmail_putFrom(email,"joe@example.com");
    CkEmail_putBody(email,"This is a test");
    CkEmail_AddTo(email,"","recipient@example.com");

    printf("%s\n",CkEmail_getMime(email));
    printf("\n");
    printf("-------------------------------------------------------------\n");
    printf("\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 = CkXml_Create();
    CkXml_putTag(xml,"DeliveryStatusFields");
    CkXml_NewChild2(xml,"Final-Recipient","rfc822; recipient@example.com");
    CkXml_NewChild2(xml,"Action","failed");
    CkXml_NewChild2(xml,"Status","5.1.2");
    CkXml_NewChild2(xml,"Diagnostic-Code","smtp; 550 5.1.2 Host unknown (Domain name not found)");
    dtNow = CkDateTime_Create();
    CkDateTime_SetFromCurrentSystemTime(dtNow);
    CkXml_NewChild2(xml,"Last-Attempt-Date",CkDateTime_getAsRfc822(dtNow,TRUE));

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

    explain = CkStringBuilder_getAsString(sbText);

    dsnEmail = CkEmail_Create();
    success = CkEmail_ToDsn(email,explain,CkXml_getXml(xml),headerOnly,dsnEmail);
    if (success == FALSE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        CkXml_Dispose(xml);
        CkDateTime_Dispose(dtNow);
        CkStringBuilder_Dispose(sbText);
        CkEmail_Dispose(dsnEmail);
        return;
    }

    printf("%s\n",CkEmail_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--


    CkEmail_Dispose(email);
    CkXml_Dispose(xml);
    CkDateTime_Dispose(dtNow);
    CkStringBuilder_Dispose(sbText);
    CkEmail_Dispose(dsnEmail);

    }