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

问题描述:

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

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

+1

链接的NuGet:https://www.nuget.org/packages/OpenPop.NET/ – razon 2017-01-13 11:50:30

通过POP3协议下载电子邮件是该任务的简单部分。该协议非常简单,如果您不想通过网络发送明文密码(并且无法使用SSL加密通信通道),则唯一难以完成的部分可能是高级身份验证方法。有关详细信息,请参阅RFC 1939: Post Office Protocol - Version 3 RFC 1734: POP3 AUTHentication command

当您必须解析收到的电子邮件时,这意味着大多数情况下解析MIME格式。您可以在几个小时或几天内编写快速的&脏MIME解析器,它将处理所有传入邮件的95%以上。提高分析器因此它可以分析几乎任何电子邮件方式:为了解决它们产生的错误和误解的RFC从最流行的邮件客户端发送

  • 越来越电子邮件样本,改善解析器。
  • 确保违反RFC的邮件标题和内容的消息会不会崩溃您的解析器和你将能够从错位的电子邮件
  • (国际化问题的正确处理如语言阅读每一个可读或可猜测值从右击写到左,正确的编码为特定的语言等)
  • UNICODE
  • 附件和层次的消息项目树看到"Mime torture email sample"
  • S/MIME(签名和加密的电子邮件)。

调试一个强大的MIME解析器需要几个月的工作。我知道,因为我正在看我的朋友为下面提到的组件编写一个这样的解析器,并且正在为它写几个单元测试;-)

回到原始问题。

code taken from our POP3 Tutorial page和链接会帮助你:

// 
// 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(); 
+7

基本上你说的是“买我的组件”,对吧?没有错,这听起来像是一个很好的组件。 – MarkJ 2009-11-06 12:31:01

+3

您可以尝试任何第三方组件(免费或商业)。我的这篇文章试图指出,编写这样的组件既困难又费时,因为需要进行大量测试 - 如果没有大量真实用户的数据进行大量错误报告,您几乎无法做到这一点。如果您选择Rebex组件,那将会很不错,但是如果您选择另一组件,我对它没有任何问题。编写自己的MIME解析器或使用Web上发现的一些概念验证代码是恕我直言,在这种情况下,不是最好的方法。但是我可能会受到偏见;-),先画出自己的结论并测试代码。 – 2009-11-06 19:21:16

+0

我可以使用Rebex组件从Exchange 2003收件箱中获取邮件吗? – Kiquenet 2010-11-23 09:46:53

叫我老的方式,但为什么要使用第三方库为一个简单的协议。我已经在基于Web的ASP.NET应用程序中使用System.Net.Sockets.TCPClient和System.Net.Security.SslStream实现了POP3读取器,以进行加密和身份验证。就协议而言,一旦开启了与POP3服务器的通信,就只有少数命令需要处理。与其合作非常简单。

我的开源应用程序BugTracker.NET包含一个可以解析MIME的POP3客户端。 POP3代码和MIME代码都来自其他作者,但您可以在我的应用程序中看到它们是如何融合在一起的。我使用http://anmar.eu.org/projects/sharpmimetools/

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

您也可以尝试Mail.dll mail component,它支持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

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

我不会推荐OpenPOP。我花了几个小时来调试一个问题 - OpenPOP的POPClient.GetMessage()神秘地返回null。我调试了这个,发现它是一个字符串索引bug - 请参阅我在此提交的补丁:http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778。找到原因很困难,因为有空的catch {}块会吞噬异常。

此外,该项目主要是处于休眠状态...的最后一个版本是在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. 我下载了this
  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(); 
     } 
    } 
}