•   Posted in: 
  • C#

The common used email client Microsoft Outlook has (as every computer specialist knows) rules for processing email. This is great, but what if you have an mailing list application which sends bulk mail and you would like to automate the process of unsubscribing the email addresses which do not exists?

I ran into the open source project Openpop.net. It’s awesome! [more]

The latest version at the moment is 2.0.2 and it is from the 3rd of March 2011. So download 2.0.2 from this location: http://sourceforge.net/projects/hpop/files/OpenPOP.NET/2.0.2/OpenPop.NET%202.0.2.zip/download

If you have questions about OpenPop, you can post it on StackOverflow and there is a good chance that Foens will answer it. He is the main developer of the project atm : http://stackoverflow.com/users/477854/foens 

Or send a mail to the news list http://sourceforge.net/projects/hpop/support

So I took the DLL and added it to my project as a reference and then added this to the using:

using OpenPop;
using OpenPop.Pop3;
using OpenPop.Mime;


And here is the rest of my snippet to process your postmaster mail after a mailing:

const string MatchEmailPattern = 
           + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
           + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
           + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";

protected void Page_Load(object sender, EventArgs e)
    Pop3Client client = new Pop3Client();
    client.Connect("pop.yourserver.com", 110, false);
    client.Authenticate("your@mailaddress.com", "yourpassword");
    for(int i = 1; i < client.GetMessageCount(); i++)
        Message msg = client.GetMessage(i);
        if (msg.Headers.Subject.Contains("Delivery Status Notification"))
            MessagePart plainTextPart = msg.FindFirstPlainTextVersion();
            string body               = plainTextPart.GetBodyAsText();

            Regex rx                = new Regex(MatchEmailPattern,  RegexOptions.Compiled | RegexOptions.IgnoreCase);
            MatchCollection matches = rx.Matches(body);
            int noOfMatches = matches.Count;

            if (noOfMatches == 1) // if it's an html mail, it has 2 matches. and it should be plain text
                // sql update your newsletter db and remove the subscription from the current mailaddress
                client.DeleteMessage(i); //marks the msg for deletion
                // redirectTheMailForManualProcessing();
            // redirectTheMailForManualProcessing();
    client.Disconnect(); // deletes the marked mails and disconnects

Please note that the for loop does not start with 0 as is common with arrays.

Have fun with the snippet!

kick it on DotNetKicks.com Shout it

Post on LinkedIn