JP Hellemons Building E-commerce web applications

Auto process your E-mail with Asp.Net C#

2. May 2011 21:11by JP Hellemons 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!

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 = 
@"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([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
}
else{
// redirectTheMailForManualProcessing();
}
}
else{
// 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

Share or Bookmark this post…
  • del.icio.us
  • Digg
  • DotNetKicks
  • eKudos
  • E-Mail
  • Facebook
  • Google
  • LinkedIn
  • msdn Social
  • Reddit
  • NuJIJ
  • Slashdot
  • TwitThis
  • StumbleUpon

Comments (2) -

Farogh Haider 10/14/2011 9:26:03 AM India #
Farogh Haider


Dear Sir,

How do I read only header of the mail without reading message body. Because there is more than 1000 mail so it will take so much time when reading all message with body.

Reply

JP Hellemons 10/17/2011 10:13:10 AM Netherlands #
JP Hellemons

Hi Farogh,

You can use the GetMessageHeaders method to only download the headers.

source: hpop.sourceforge.net/exampleExamineHeaders.php

Good luck!

Reply

Pingbacks and trackbacks (2)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading