What methods are there for having .NET code run and handle e-mails as they arrive?

后端 未结 4 1428
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 02:03

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.

相关标签:
4条回答
  • 2021-01-15 02:51

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-15 03:02

    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#".

    0 讨论(0)
  • 2021-01-15 03:06

    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

    0 讨论(0)
提交回复
热议问题