views:

790

answers:

6

How do I programmatically read an incoming email with .NET. I need a method to take the content of email message (in this case XML) on a POP server and read it into my application.

Ideally this could be solved by:

  • .NET code that I can run as a background task on my webserver to process the email.
  • A service that can POST the contents of the email to my webserver.

Although I'm open to other options.

EDIT: Copied from the "non-answer":

Currently, we're looking at using the email accounts our hosting company provides.

Our own mail server could potentially be an option, but its something the we'd need to contact the host about.

+1  A: 

There are quite a few options, in case you have a service bus to your disposal it mostly comes with a pop3 adapter.

Or you can use a pop3 api (easymail from quiksoft is quite a good one). It also has a great product for processing bounces.

To read all the SMTP messages. If you own the mailserver, depending on the type of server there other ways to get to the messages, sometimes custom Apis and plugins, or the filesystem. Do you host your own mail server? Which mailserver do you use?

If possible I would not choose for the POP protocol, it has some limitations amongst other things like only having one folder, "getting/reading" a message removes the message from the folder (which means one process to read the messages) and other things. Depending on how many mails you need to parse it might still work for you. IMAP would already be an improvement.

With the quiksoft IMAP component:

//create a new IMAP4 object, connect 
//to a IMAP4 server and login 
IMAP4 imap = new IMAP4();
imap.Connect("mail.yourdomain.com");
imap.Login("mailbox", "password");

//select the inbox and download the envelopes
imap.SelectMailbox("Inbox");
EnvelopeCollection imapEnvelopes;
imapEnvelopes = imap.GetEnvelopes();

//loop through each message
//and output the subject
foreach (Envelope imapEnvelope in imapEnvelopes)
{
   //write the subject out to the console
   Console.WriteLine(imapEnvelope.Subject);
}

//close resources
imap.Logout();
Raymond Roestenburg
+2  A: 

We have actually just implemented the same kind of thing.

We process the content of email messages and push the data in to our CRM via a web service. We use c# with .net 3.5

To process the mail we went with IMap . There are a few .net client libraries on CodeProject. I think we used the one from LumiSoft .

We tried WebDav but didn't have much luck. This left us with Pop3 or IMap. IMap supports folders, which we needed, so we went with that. I'm sure it would be easy to do the same thing with POP3 if your server does not support IMap.

Basically we connect our Exchange server every hour and pull down any new email and process them. So far it's working well.

Edit: We also use SharpMimeTools to get the raw emails in to a more usable format.

Jamie
+1  A: 

There are many free POP3 client implementations for .Net.

For example: this one.

Sunny
A: 

IceWarp's Merak email server was great for this. It let us setup an special kind of account called an executable account. You associate a .exe with the email address and pick a bunch of things to send on the command line. When an email get sent to that account, it fires off the executable. The .exe has to be a console app and you must take care that it doesn't hang and what not, but I thought it was the perfect solution.

We use it to handle processing stats from google analytics and bounce backs from other mail servers.

Al W
+1 for the Merak mail server, it is a great product. The way it allows for a dll plugin is also great for processing the bounces.
Raymond Roestenburg
A: 

You could always DIY A POP 3 Class POP is one of the easier protocols

mikej