-->

How do I read incoming mail using C# [closed]

2019-01-23 20:37发布

问题:

I am looking for a solution to allow me to read incoming emails. The three methods I can think of to do this at the moment are:

  1. Create an Email Server parsing emails
  2. Hook into an existing exchange server
  3. Hook into outlook that is already set up with an email account

What is the best way to do this? And how does one go about implementing it?

Thanks

回答1:

If you are already dealing with an Exchange server as the mailbox host I would suggest leveraging that via IMAP (preferred) or POP access. Recently I developed a solution that accesses a specified mailbox via AfterLogic's MailBee.NET IMAP component which I think is worth the recommendation. They have a standard trial version and reasonable pricing. Also if you go this route either POP or IMAP automation is flexible enough to work with almost any mailbox server platform; It doesn't have to be limited to Exchange environments.

There are also free .NET IMAP components out there that may do the job as well. In my limited research I found that the free alternatives didn't quite meet all of my requirements or were not as easy to learn but your situation may differ. For completeness, here is a list of alternative / free IMAP libraries I considered before deciding to spend the money on MailBee:

  • ImapX - http://hellowebapps.com/products/imapx/
  • LumiSoft.Net Imap Client - http://www.lumisoft.ee/lswww/download/downloads/Net/
  • InterIMAP - http://interimap.codeplex.com/

To address the 2nd part of your question... The implementation in my recent project involved writing a very simple console application that references the MailBee.NET IMAP library. The console application has a standard config file and accepts command line arguments as parameters. We define Windows scheduled tasks to run the console application according to our process needs. I am sure you could do this any number of other ways but this was the simplest approach for our needs.



回答2:

I know this is an older thread but I wanted to add a link to a great open source Imap library called ImapX2: http://imapx.codeplex.com/

I tried most of the links in this thread to varying degrees of success but I found that the ImapX2 library work the best according to my needs.



回答3:

Use existing IMAP server and IMAP component for that.

Creating your own IMAP server is a big security risk in my opinion.

There are free components, but most of them have problems with national characters and 'not standard' email messages...and there is none support (LumiSoft.Net is abandoned for almost 2 years)

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

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

Mail.dll also includes SMTP component and template engine so you can send replies easily.

Disclaimer: I'm involved in the development of this commercial product.