Parsing Bounced Emails
Page 1 Page 2 Page 3
In some cases examining only the message properties might provide sufficient evidence of a bounced message. Depending on your particular requirements, however, you may want to extract the offending email address for further processing. We'll look a little deeper at our failure notice sent by our example SMTP server for additional evidence.
using System;
using System.Collections;
using System.IO;
using System.Text;
using Jscape.Email;
namespace EmailExample {
public class EmailExample {
public EmailExample(string hostname, string username, string password) {
// ... Instantiate POP3 ...
myPop = new Pop(this.hostname, this.username, this.password);
// ... Turn on debug mode ...
myPop.Debug = true;
// ... Override delete messages setting ...
myPop.DeleteMessages = false;
// ... and Subscribe to pop events.
myPop.ConnectedEvent += new Pop.ConnectedEventHandler(OnConnected);
myPop.DisconnectedEvent += new Pop.DisconnectedEventHandler(OnDisconnected);
// connect to pop server
myPop.Connect();
string offendingEmail = ""; // stores the offending email address, if any.
int mc = 0; // message counter used to delete offending message after processing.
// iterate through all email messages
IEnumerator e = myPop.GetMessages();
while(e.MoveNext()) {
mc++; // message indexes are 1-based.
EmailMessage message = (EmailMessage)e.Current;
// first test message properties for clues.
if ( (message.From.ToLower().IndexOf("mailer-daemon") > 0) ||
(message.From.ToLower().IndexOf("mail delivery subsystem") > 0) ||
(message.Subject.ToLower().IndexOf("returned") > 0) ) {
// this is a suspect message...
// message part(0) = response message from server
string msgLine = "";
StringReader sr = new StringReader(message.GetPart(0).GetBody());
while ((msgLine = sr.ReadLine()) != null) {
EmailAddress eAddr = new EmailAddress(msgLine);
// if a valid email address is found...
if (eAddr.Valid) {
offendingEmail = eAddr.Email;
Console.WriteLine("*** Offending Email Address\r\n\t" +
offendingEmail + "\r\n***");
break;
}
}
sr.Close();
}
if (offendingEmail != "") {
Console.WriteLine("\tDelete " + offendingEmail + " from database.\r\n");
Console.WriteLine("Deleting suspect message from POP server.");
myPop.DeleteMessage(mc);
}
}
// your server may require a slight delay in order to respond.
Thread.Sleep(100);
// disconnect from server
myPop.Disconnect();
}
[STAThread]
static void Main() {
// Ensure that you use proper credentials
EmailExample bounced = new EmailExample("hostname", "username", "password");
}
public void OnConnected(object sender, PopConnectedEventArgs e) {
Console.WriteLine("Connected to {0}", e.Host);
}
public void OnDisconnected(object sender, PopDisconnectedEventArgs e) {
if (myPop.IsConnected()) {
myPop.Disconnect();
}
Console.WriteLine("Disconnected.");
}
}
}
Here we extract the offending email address from the first part of the message, which is the message body of the response email sent from our example SMTP server. Since the message body contains the offending email address on a separate line, it is a simple matter to validate each line as a properly formed email address. If found, then the email address can be removed from a database and the email message removed from the server. Figure 2 illustrates this process.
As you can see from this example, we can parse email messages for those that meet much stricter bounce criteria. Email Factory for .NET exposes additional classes and methods, such as MimeHeaderAttr and GetAttachments() that can aid in parsing bounced email messages. To see what else Email Factory for .NET has to offer Download a FREE 30 day Email Factory for .NET Evaluation.
Page 1 Page 2 Page 3
|