Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Parse a Delivery Status Notification (DSN) EmailDownload: Chilkat .NET Assemblies This C# example program demonstrates how to parse a DSN message using the Chilkat Email component. // How to parse a DSN Email // (Parsing a Delivery Status Notification Email) // Unlock the components that are needed for this example: Chilkat.MailMan mailman0 = new Chilkat.MailMan(); mailman0.UnlockComponent("30-day trial"); Chilkat.Mime mime0 = new Chilkat.Mime(); mime0.UnlockComponent("30-day trial"); // Load a .eml file into the email object. // A zip containing 10 DSN sample emails can be found at: // http://www.chilkatsoft.com/dsn_samples/DSN_Samples.zip Chilkat.Email email = new Chilkat.Email(); email.LoadEml("dsnSample10.eml"); // The email should be a multipart/report. If not, exit. if (!email.IsMultipartReport()) { MessageBox.Show("Not a DSN!"); return; } // The overall format of a DSN is described here: // http://www.chilkatsoft.com/braindump/email_headers/mime_format_dsn.html // // In summary, the outermost MIME layer is a multipart/report, // and 3 parts are contained within: // 1) A human-readable explanation of the DSN (content-type is text/plain) // 2) A message/delivery-status part. // 3) The original email (message/rfc822) or the headers of the original // email (text/rfc822-headers) // The human-readable part of the DSN is accessible via the email's Body property: textBox1.Text = email.Body; // To parse the message/delivery-status part, we need Chilkat Mime. // Get the email as a MIME object: Chilkat.Mime mime = email.GetMimeObject(); // Now get the message/delivery-status part. // It is the 2nd sub-part, so the index is 1. Chilkat.Mime dstat = mime.GetPart(1); // Do we have what we expect? if (dstat.ContentType.Equals("message/delivery-status")) { // The body of the message/delivery-status contains MIME headers. // There is a single per-message header set, and 1 or more // per-recipient header sets. // For example, this one has 2 recipients: // //Reporting-MTA: dns; comcast.net //Arrival-Date: 13 Sep 2006 23:28:50 +0000 // //Final-Recipient: rfc822; <ajsfdlkjasdglkjadsg@blah.com> //Action: failed //Status: 5.0.0 550_Requested_action_not_taken:_mailbox_unavailable_or_not_local //Diagnostic-Code: smtp; Permanent Failure: Other undefined Status //Last-Attempt-Date: Wed, 13 Sep 2006 23:28:51 -0000 // //Final-Recipient: rfc822; <asgfafdsga8fgsfdgk@blah.com> //Action: failed //Status: 5.0.0 550_Requested_action_not_taken:_mailbox_unavailable_or_not_local //Diagnostic-Code: smtp; Permanent Failure: Other undefined Status //Last-Attempt-Date: Wed, 13 Sep 2006 23:28:51 -0000 // How do we parse it??? // We'll let ChilkatMime do the work. First, we'll get the body and then // split it into separate chunks where each is then treated as a separate // MIME message with an empty body. string body = dstat.GetBodyDecoded(); // The Chilkat.StringArray class includes the SplitAndAppend method to // split a string using boundary strings. If we split at double-CRLF's // we can get each chunk. // Note: The SplitAndAppend is new to Chilkat.NET (14-Sep-2006). If it is not present // in your existing DLL, download the latest from http://www.chilkatsoft.com/downloads.asp Chilkat.StringArray sa = new Chilkat.StringArray(); sa.SplitAndAppend(body, "\r\n\r\n"); MessageBox.Show(Convert.ToString(sa.Count)); // Display each set of headers... int i; for (i=0; i<sa.Count; i++) { MessageBox.Show("[" + sa.GetString(i) + "]"); } // The 1st header-set contains the Reporting-MTA. // Load it and Chilkat.Mime mTemp = new Chilkat.Mime(); mTemp.LoadMime(sa.GetString(0) + "\r\n\r\n"); // Display the Reporting-MTA listBox1.Items.Add("Reporting-MTA: " + mTemp.GetHeaderField("Reporting-MTA")); // Display the Arrival-Date listBox1.Items.Add("Arrival-Date: " + mTemp.GetHeaderField("Arrival-Date")); listBox1.Items.Add("--"); // Display information for each Final-Recipient for (i=1; i<sa.Count; i++) { mTemp.LoadMime(sa.GetString(i)); // (Not all of these fields may be present.) // Information about Final-Recipient is located here: // http://chilkatsoft.com/braindump/email_headers/Final-Recipient_header.html listBox1.Items.Add("Final-Recipient: " + mTemp.GetHeaderField("Final-Recipient")); // Information about the Action header field is located at: // http://chilkatsoft.com/braindump/email_headers/action_dsn_header_field.html listBox1.Items.Add("Action: " + mTemp.GetHeaderField("Action")); // Information about the Status header field is located at: // http://chilkatsoft.com/braindump/email_headers/status_dsn_header_field.html listBox1.Items.Add("Status: " + mTemp.GetHeaderField("Status")); // Information about the Diagnostic-Code header field is located at: // http://chilkatsoft.com/braindump/email_headers/diagnostic-code_header_field.html listBox1.Items.Add("Diagnostic-Code: " + mTemp.GetHeaderField("Diagnostic-Code")); listBox1.Items.Add("Last-Attempt-Date: " + mTemp.GetHeaderField("Last-Attempt-Date")); listBox1.Items.Add("Remote-MTA: " + mTemp.GetHeaderField("Remote-MTA")); listBox1.Items.Add("--"); } } else { MessageBox.Show("Unexpected delivery status, content-type = " + dstat.ContentType); } // The original email is available as an embedded message: Chilkat.Email originalEmail = email.GetAttachedMessage(0); // If the original email is Nothing, then the DSN only included the original // email headers as a text/rfc822-headers if (originalEmail != null) { // We got the original email. Because it is a ChilkatEmail2 object, // the headers, bodies, attachments, etc, are all accessible. // This example will simply save the original email to a file. originalEmail.SaveEml("originalEmail.eml"); } else { // The DSN did not have the complete message/rfc822 attached. // Use Chilkat Mime to see what we have... // (The DSN samples at http://www.chilkatsoft.com/dsn_samples/DSN_Samples.zip // include a DSN (sampleEml10.eml) that uses a text/rfc822-headers.) // Access the email via the ChilkatMime API: mime = email.GetMimeObject(); // Now get the 3rd sub-part, which is at index 2. Chilkat.Mime part3 = mime.GetPart(2); // Make sure we have it... if (part3 == null) { MessageBox.Show("Did not get sub-part #3!"); return; } // Is this a text/rfc822-headers part? if (part3.ContentType.Equals("text/rfc822-headers")) { // OK, the body of this part is a collection of headers. // If we get the body, we can append a few blank lines // and then read it as a MIME message and access any // header. string msg = part3.GetBodyDecoded() + "\r\n\r\n"; // Load it into a MIME object. Chilkat.Mime mime2 = new Chilkat.Mime(); mime2.LoadMime(msg); // Fetch some of the headers: listBox1.Items.Add("From: " + mime2.GetHeaderField("from")); listBox1.Items.Add("Subject: " + mime2.GetHeaderField("subject")); listBox1.Items.Add("To: " + mime2.GetHeaderField("to")); } else { MessageBox.Show("Unexpected content-type: " + part3.ContentType); } } Important: The download for this
example does not contain the ChilkatDotNet.dll which |
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.