使用Lesnikowski访问POP3服务器并获得邮件

 

      最近做的项目需要下载邮件并且进行分析,因此需要有一个能访问POP3服务器并取得邮件的程序。在网上找了下相关的程序,一般都是采用TcpClient的方法,连接到POP3服务器后,通过流发送相应的命令完成与服务器的互动,相关的命令和流程大致如下:

Client : +OK Server POP Ready!!
Client : USER xxx
Server : +OK
Client : PASS yyy
Server : +OK user successfully logged on
Client : STAT
Server : +OK n m
Client : RETR 1
Server : +OK
---{ data }-----
Client : QUIT
Server : +OK Server POP signing off

 

    程序内容大致如下:

 

声明公共变量:

  1. public TcpClient Server;
  2. public NetworkStream NetStrm;
  3. public StreamReader RdStrm;
  4. public string Data;
  5. public byte[] szData;
  6. public string CRLF = "/r/n";

下面是连接POP3服务器的代码:

  1. private void ConnectBtn_Click(object sender, System.EventArgs e)
  2. {
  3. // change cursor into wait cursor
  4. Cursor cr = Cursor.Current;
  5. Cursor.Current = Cursors.WaitCursor;
  6. // create server POP3 with port 110
  7. Server = new TcpClient(POPServ.Text,110);
  8. Status.Items.Clear();
  9. try
  10. {
  11. // initialization
  12. NetStrm = Server.GetStream();
  13. RdStrm= new StreamReader(Server.GetStream());
  14. Status.Items.Add(RdStrm.ReadLine());
  15. // Login Process
  16. Data = "USER "+ User.Text+CRLF;
  17. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  18. NetStrm.Write(szData,0,szData.Length);
  19. Status.Items.Add(RdStrm.ReadLine());
  20. Data = "PASS "+ Passw.Text+CRLF;
  21. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  22. NetStrm.Write(szData,0,szData.Length);
  23. Status.Items.Add(RdStrm.ReadLine());
  24. // Send STAT command to get information ie: number of mail and size
  25. Data = "STAT"+CRLF;
  26. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  27. NetStrm.Write(szData,0,szData.Length);
  28. Status.Items.Add(RdStrm.ReadLine());
  29. // change enabled - disabled button
  30. ConnectBtn.Enabled = false;
  31. DisconnectBtn.Enabled = true;
  32. RetrieveBtn.Enabled = true;
  33. // back to normal cursor
  34. Cursor.Current = cr;
  35. }
  36. catch(InvalidOperationException err)
  37. {
  38. Status.Items.Add("Error: "+err.ToString());
  39. }

下面是断开服务器的代码:

  1. private void DisconnectBtn_Click(object sender, System.EventArgs e)
  2. {
  3. // change cursor into wait cursor
  4. Cursor cr = Cursor.Current;
  5. Cursor.Current = Cursors.WaitCursor;
  6. // Send QUIT command to close session from POP server
  7. Data = "QUIT"+CRLF;
  8. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  9. NetStrm.Write(szData,0,szData.Length);
  10. Status.Items.Add(RdStrm.ReadLine());
  11. //close connection
  12. NetStrm.Close();
  13. RdStrm.Close();
  14. // change enabled - disabled button
  15. ConnectBtn.Enabled = true;
  16. DisconnectBtn.Enabled = false;
  17. RetrieveBtn.Enabled = false;
  18. // back to normal cursor
  19. Cursor.Current = cr;
  20. }

以下是取得某封特定邮件内容的代码:

  1. private void RetrieveBtn_Click(object sender, System.EventArgs e)
  2. {
  3. // change cursor into wait cursor
  4. Cursor cr = Cursor.Current;
  5. Cursor.Current = Cursors.WaitCursor;
  6. string szTemp;
  7. Message.Clear();
  8. try
  9. {
  10. // retrieve mail with number mail parameter
  11. Data = "RETR "+ Number.Text+CRLF;
  12. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  13. NetStrm.Write(szData,0,szData.Length);
  14. szTemp = RdStrm.ReadLine();
  15. if(szTemp[0]!='-'
  16. {
  17. while(szTemp!=".")
  18. {
  19. Message.Text += szTemp;
  20. szTemp = RdStrm.ReadLine();
  21. }
  22. }
  23. else
  24. {
  25. Status.Items.Add(szTemp);
  26. }
  27. // back to normal cursor
  28. Cursor.Current = cr;
  29. }
  30. catch(InvalidOperationException err)
  31. {
  32. Status.Items.Add("Error: "+err.ToString());
  33. }
  34. }

 

   这样的过程存在一些缺点:1。程序实现复杂;2。需要使用流从服务器将数据完全下载下来后才能对邮件内容进行解析,这样如果邮件很大或者邮件数量过多的话需要耗费大量的时间。

    在《RSS and Atom in Action》一书里面有说到一个.net下的邮件处理程序集Lesnikowski,可以简化以上的过程。但是在使用《RSS and Atom in Action》书的网站上提供的那个Lesnikowski.Pawel.Mail.Pop3.dll时发现可以解析来自tom.com的邮件,但是在解析来自公司邮箱的邮件时居然找不到邮件的Body...而且还存在一个问题,这个程序集中的函数不能自动对Base64编码过的邮件进行解码。现在Lesnikowski提供了新的版本,试验了下解决了Body为null的问题,而且可以自动对Base64进行解码。但是新的版本需要购买,否则一些功能用不了,而且会是不是在邮件正文里面加一句“请购买。。。。。”的提示。不过这样还好,反正用不了的功能我也不需要,我只需要用到它提供的两个属性:TextDataString和HtmlDataString还是可以用的,一个会忽略掉HTML标签只返回文本,一个会返回带有HTML标签的文本,还省去了我解析格式化的邮件正文的麻烦。至于时不时加入一句提醒购买的话也是可以解决掉的问题,因此可以暂时先凑合着用 -   -  。以下是官方上的一个使用Lesnikowski连接POP3服务器的例子:

  1. using Lesnikowski.Client;   
  2. using Lesnikowski.Mail;   
  3. using Lesnikowski.Mail.Headers;   
  4. using Lesnikowski.Mail.Headers.Constants;   
  5. using System;   
  6.   
  7. class Program   
  8. {   
  9.     static void Main(string[] args)   
  10.     {   
  11.         Pop3 pop3 = new Pop3();   
  12.         pop3.User = "lesnikowski";                   // Set user name and password   
  13.         pop3.Password = "password";   
  14.   
  15.         try  
  16.         {   
  17.             pop3.Connect("mail.host.com");           // Connect to server and login   
  18.             pop3.Login();   
  19.             pop3.GetAccountStat();                   // Get account statistics   
  20.   
  21.             SimpleMailMessageBuilder builder = new SimpleMailMessageBuilder();   
  22.   
  23.             for(int i=1; i<=pop3.MessageCount; i++)   
  24.             {   
  25.                 try                                           
  26.                 {   
  27.                     // Receive mail   
  28.                     ISimpleMailMessage simpleMail = builder.CreateFromEml(   
  29.                         pop3.GetMessage(i)    
  30.                         );   
  31.   
  32.                     // Write out received mail message   
  33.                     Console.WriteLine( "-- email {0} --", i);   
  34.                     Console.WriteLine( simpleMail.Subject );   
  35.                     Console.WriteLine( simpleMail.TextDataString );   
  36.   
  37.                     // Use other properties to access mail data   
  38.                 }   
  39.                 catch(MailException ex)   
  40.                 {   
  41.                     Console.WriteLine( ex.Message ); // Invalid mail format   
  42.                 }   
  43.             }   
  44.         }                               
  45.         catch(ServerException ex)   
  46.         {   
  47.             Console.WriteLine( ex.Message );         // Server error   
  48.         }   
  49.         finally  
  50.         {   
  51.             pop3.Close(false);                       // Close connection   
  52.         }   
  53.         Console.WriteLine( "Receive end./n Press enter..." );   
  54.         Console.ReadLine();   
  55.     }   
  56. };   

 

下面的网站上提供了老的dll的实现方式,基本也是按照最上面的通过流的方式来实现的,对于了解这个程序集的后台实现原理比较有帮助。

https://dasblogce.svn.sourceforge.net/svnroot/dasblogce/trunk/source/Lesnikowski.Pawel.Mail.Pop3/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值