tags:

views:

227

answers:

5

I've been tasked with creating some sort of service that will take any e-mail sent to an e-mail address and handle the contents of the e-mail (including binary attachments.)

I've got full access to the server (Windows Server 2008) that will run this service, and have decided to code the solution using the .NET framework (3.5).

Now I'm thinking about the different ways I can do this. Since I'm not very experienced in handling e-mails programmatically, the first solution that came into my head was to create an e-mail client that will periodically poll an existing e-mail server for incoming e-mail (using the POP3 protocol) and process them. But there are other ways to do it.

One could be to use IMAP to stay connected to the mail server and fetch e-mails as soon as they arrive (more responsive, but I believe the IMAP protocol is harder to implement.) The follow-up question here is: where can I find reliable e-mail libraries that support the POP3 or the IMAP protocol?

Another could be to somehow configure the e-mail server to directly pipe the e-mails sent to a specific address to my binary code (I've seen this done on Linux servers.) I have no idea how to go about this, though.

The last I can think of would be to create a dummy e-mail server on its own address that handles the e-mail directly as it arrives, but to me this seems like a bad idea.

Does anyone have experience in this area? I would imagine that having to write e-mail handlers for ticket support systems isn't that uncommon, all I need in addition to that is to handle the attachments.

I'd be grateful for any hints and tips.

A: 

I've used webdav in the past with c# to access an exchange server periodically and process emails.

This has worked quite well, and I'd probably use that method again if I need to do it.

Bravax
A: 

Some of the .net components from http://www.quiksoft.com/ might help with your requirement.

The app polls a POP3 mail server every x minutes(s) and works it's way through the messages in the queue, and deletes them when it's processed each msg.

The QuikSoft tools also provide ways to parse the emails to get the content from each msg.

+1  A: 

As with alot of things - it depends. Ask yourself the following questions:

  1. What are your latency requirements--do you need to process incoming messages as quickly as possible, or can processing be batched? If it can be batched, then how often would you have to process the "inbox"?

  2. What are your throughput requirements? How many messages are we talking about per minute here? How big are the messages? This would affect the decision you make about polling interval if using a batch scenario;

  3. What sort of e-mail system are you integrating with? If it's Exchange, what programmatic interfaces are available to access a mailbox? Until the most recent version of Exchange, interestingly enough, there were issues with accessing a mailbox on an Exchange server (The client-side CDO COM components needed to be used which is not ideal---and there were security limitations).

By far the simplest approach is to poll a mailbox using POP3. However, if you need to respond immediately to an incoming message, then this isn't going to cut it.

As far as possible avoid writing your own SMTP service--it's been done a thousand times before and you're just creating unnecessary work for yourself and exposing yourself to security threats. If you absolutely have to respond immediately to messages, then rather set up an instance of Sendmail or Postfix to spawn a process that you have written.

If you're going to go for the POP3 solution (it looks like you are), then have a read of related questions "Free POP3 .NET library?" and "Reading Email using POP3 in C#".

Eric Smith
There are no real latency requirements, although it would be nice to process them as they arrive to spread out CPU usage as much as possible. This won't be a server that will get much throughput. There will be a few usage spikes (this is for a contest), but after that, use will be pretty low. Messages will be up to 1 MB each (they include a short mobile video.) The e-mail system to integrate with hasn't been decided yet, but I don't think it will be Exchange. And finally, I intend to do this with as much code reuse as possible so pointers to libraries are appreciated.
Blixt
A: 

Receiving the email is not the hardest part, parsing, extracting attachments is.

If any one is interested in commercial product take a look at Mail.dll. It supports IDLE command you have mentioned for instant notifications.

Mail.dll includes POP3, IMAP clients and powerful MIME parser:

using(Imap imap = new Imap())
{
    imap.Connect("imap.server.com");
    imap.Login("user", "password");

    imap.SelectInbox();
    List<long> uidList = imap.SearchFlag(Flag.Unseen);
    foreach (long uid in uidList)
    {
        IMail mail = new MailBuilder()
            .CreateFromEml(imap.GetMessageByUID(uid));
        Console.WriteLine(mail.Subject);
    }
    imap.Close(true);
}

Please note that this is commercial product that I've created.

You can download it at http://www.lesnikowski.com/mail

Pawel Lesnikowski