在C#中使用Pop3阅读电子邮件

我正在寻找一种在C#2.0中使用Pop3阅读电子邮件的方法。 目前,我正在使用CodeProject中find的代码 。 但是,这个解决scheme并不理想。 最大的问题是它不支持用unicode编写的邮件。

我已经成功地使用OpenPop.NET通过POP3访问电子邮件。

通过POP3协议下载电子邮件是该任务的简单部分。 该协议非常简单,如果您不想通过networking发送明文密码(并且不能使用SSLencryption通信通道),唯一困难的部分可能是高级authentication方法。 有关详细信息,请参阅RFC 1939:邮局协议 – 版本3和RFC 1734:POP3 AUTHentication命令 。

当您必须parsing收到的电子邮件时,难点在于大多数情况下parsingMIME格式。 您可以在几个小时或几天内编写快速肮脏的MIMEparsing器,它将处理所有传入消息的95%以上。 改进parsing器,使其可以parsing几乎所有的电子邮件:

  • 从最stream行的邮件客户端发送电子邮件样本,并改进parsing器,以解决由它们产生的错误和RFC误解。
  • 确保消息违反RFC的消息标题和内容不会崩溃您的parsing器,并且您将能够读取每个可读或可猜测的价值从损坏的电子邮件
  • 正确处理国际化问题(如从左至右写的语言,针对特定语言的正确编码等)
  • UNICODE
  • 在“模仿酷刑邮件样本”中看到的附件和分层消息项目树
  • S / MIME(签名和encryption的电子邮件)。
  • 等等

debugging一个健壮的MIMEparsing器需要几个月的时间。 我知道,因为我正在看我的朋友为下面提到的组件编写一个这样的parsing器,并正在为它写几个unit testing;-)

回到原来的问题。

以下代码取自我们的POP3教程页面和链接将帮助您:

// // create client, connect and log in Pop3 client = new Pop3(); client.Connect("pop3.example.org"); client.Login("username", "password"); // get message list Pop3MessageCollection list = client.GetMessageList(); if (list.Count == 0) { Console.WriteLine("There are no messages in the mailbox."); } else { // download the first message MailMessage message = client.GetMailMessage(list[0].SequenceNumber); ... } client.Disconnect(); 
  • HOWTO:从C#中的GMail帐户下载电子邮件 (blogpost)
  • .NET的Rebex Mail(用于.NET的POP3 / IMAP客户端组件)
  • 用于.NET的Rebex Secure Mail(用于.NET的POP3 / IMAP客户端组件 – 启用SSL)

我的开源应用程序BugTracker.NET包含一个可以parsingMIME的POP3客户端。 POP3代码和MIME代码都来自其他作者,但你可以看到它是如何融合在我的应用程序。

对于MIMEparsing,我使用http://anmar.eu.org/projects/sharpmimetools/

请参阅文件POP3Main.cs,POP3Client.cs和insert_bug.aspx

您也可以尝试Mail.dll邮件组件 ,它具有SSL支持,unicode和多国电子邮件支持:

 using(Pop3 pop3 = new Pop3()) { pop3.Connect("mail.host.com"); // Connect to server and login pop3.Login("user", "password"); foreach(string uid in pop3.GetAll()) { IMail email = new MailBuilder() .CreateFromEml(pop3.GetMessageByUID(uid)); Console.WriteLine( email.Subject ); } pop3.Close(false); } 

你可以在https://www.limilabs.com/mail下载;

请注意,这是我创build的商业产品。

叫我旧时尚,但为什么使用一个简单的协议第三方库。 我已经在基于Web的ASP.NET应用程序中使用System.Net.Sockets.TCPClient和System.Net.Security.SslStream实现了POP3读取器,以进行encryption和身份validation。 就协议而言,一旦你打开了与POP3服务器的通信,就只有less数你必须处理的命令。 合作是一个非常简单的协议。

我不会推荐OpenPOP。 我花了几个小时debugging一个问题 – OpenPOP的POPClient.GetMessage()神秘地返回null。 我debugging了这个,发现它是一个string索引错误 – 请参阅我在这里提交的补丁: http : //sourceforge.net/tracker/ ?func=detail&aid=2833334&group_id=92166&atid=599778。 find原因很困难,因为有空的catch {}块会吞噬exception。

而且,这个项目大部分都是hibernate状态,最后一个版本是在2004年。

现在我们仍然在使用OpenPOP,但是我们来看看一些人们在这里推荐的其他项目。

HigLabo.Mail易于使用。 以下是一个示例用法:

 using (Pop3Client cl = new Pop3Client()) { cl.UserName = "MyUserName"; cl.Password = "MyPassword"; cl.ServerName = "MyServer"; cl.AuthenticateMode = Pop3AuthenticateMode.Pop; cl.Ssl = false; cl.Authenticate(); ///Get first mail of my mailbox Pop3Message mg = cl.GetMessage(1); String MyText = mg.BodyText; ///If the message have one attachment Pop3Content ct = mg.Contents[0]; ///you can save it to local disk ct.DecodeData("your file path"); } 

你可以从https://github.com/higty/higlabo或者Nuget [HigLabo]

我刚刚尝试SMTPop,它的工作。

  1. 我下载了这个 。
  2. smtpop.dll引用添加到我的C#.NET项目中

写下面的代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using SmtPop; namespace SMT_POP3 { class Program { static void Main(string[] args) { SmtPop.POP3Client pop = new SmtPop.POP3Client(); pop.Open("<hostURL>", 110, "<username>", "<password>"); // Get message list from POP server SmtPop.POPMessageId[] messages = pop.GetMailList(); if (messages != null) { // Walk attachment list foreach(SmtPop.POPMessageId id in messages) { SmtPop.POPReader reader= pop.GetMailReader(id); SmtPop.MimeMessage msg = new SmtPop.MimeMessage(); // Read message msg.Read(reader); if (msg.AddressFrom != null) { String from= msg.AddressFrom[0].Name; Console.WriteLine("from: " + from); } if (msg.Subject != null) { String subject = msg.Subject; Console.WriteLine("subject: "+ subject); } if (msg.Body != null) { String body = msg.Body; Console.WriteLine("body: " + body); } if (msg.Attachments != null && false) { // Do something with first attachment SmtPop.MimeAttachment attach = msg.Attachments[0]; if (attach.Filename == "data") { // Read data from attachment Byte[] b = Convert.FromBase64String(attach.Body); System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false); //BinaryFormatter f = new BinaryFormatter(); // DataClass data= (DataClass)f.Deserialize(mem); mem.Close(); } // Delete message // pop.Dele(id.Id); } } } pop.Quit(); } } }