Parsing Bounced Emails
Page 1 Page 2 Page 3
We'll begin by creating a new C# console application called EmailExample. In it we'll need to add the Jscape.Email.dll reference and include the following basic structure. See the Email Factory for .NET User Guide for more information about adding a reference to your project.
using System;
using System.Collections;
using System.IO;
using System.Text;
using Jscape.Email;
namespace EmailExample {
public class EmailExample {
public EmailExample() {
}
[STAThread]
static void Main() {
}
}
}
Email Factory for .NET exposes many properties and methods that allow you to connect to a POP server, trap various events, and retrieve email messages and attachments. Now that we have the basic structure in place, we can begin adding our code to accomplish these tasks.
To create a Pop instance we'll pass the hostname, username, and password as parameters to the constructor. You will need to pass in the appropriate parameters for your server. You can review the API documentation for more information.
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);
}
[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.");
}
}
}
Now that we have a completed structure we can start processing the email messages on our example POP server. We'll begin by parsing the email message using the simplest form -- Check the message properties for suspect values.
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();
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...
Console.WriteLine("Suspect message found. Deleting.");
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.");
}
}
}
Only the message properties were checked to match our example message. If the properties match then we can remove the email message from the server. Figure 1 illustrates this process.

Page 1 Page 2 Page 3
|