2008-09-04 176 views

回答

71

我已成功使用OpenPop.NET通過POP3訪問電子郵件。

+1

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

15

通過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

2

叫我老的方式,但爲什麼要使用第三方庫爲一個簡單的協議。我已經在基於Web的ASP.NET應用程序中使用System.Net.Sockets.TCPClient和System.Net.Security.SslStream實現了POP3讀取器,以進行加密和身份驗證。就協議而言,一旦開啓了與POP3服務器的通信,就只有少數命令需要處理。與其合作非常簡單。

8

我的開源應用程序BugTracker.NET包含一個可以解析MIME的POP3客戶端。 POP3代碼和MIME代碼都來自其他作者,但您可以在我的應用程序中看到它們是如何融合在一起的。我使用http://anmar.eu.org/projects/sharpmimetools/

請參閱文件POP3Main.cs,POP3Client.cs和insert_bug.aspx

5

您也可以嘗試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

請注意,這是我創建的商業產品。

3

我不會推薦OpenPOP。我花了幾個小時來調試一個問題 - OpenPOP的POPClient.GetMessage()神祕地返回null。我調試了這個,發現它是一個字符串索引bug - 請參閱我在此提交的補丁:http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778。找到原因很困難,因爲有空的catch {}塊會吞噬異常。

此外,該項目主要是處於休眠狀態...的最後一個版本是在2004年

現在我們仍然在使用OpenPOP,但我會看看其他一些項目的人都推薦這裏。

3

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]

2

我只是想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(); 
     } 
    } 
}