LumiSoft收取邮件(含邮件附件)

来源:互联网 发布:数据字典模板下载 编辑:程序博客网 时间:2024/06/06 04:02

转自:http://blog.csdn.net/chinacsharper/article/details/21749763


在.NET当中利用C#发送电子邮件很简单,微软也提供了默认的实现,但是收取电子邮件的操作却并没有提供解决方案。好在有一些第三方的解决方案可供选择,来简化程序员日常项目中的开发工作。

这里我选用LumiSoft,下载地址为http://www.lumisoft.ee/lswww/download/downloads/。当你打开链接,会发现里面有很多项目,为了实现收取邮件的功能,我们可以选择Examples里面的pop3_client_app.zip文件。我们解压缩,利用VS打开这个项目运行即可。这是官方提供的一个现成的示例,能够收取邮件并显示。


我们打开这个项目bin目录下的LumiSoft.Net.dll文件,这就是封装好的一个核心dll。我们可以利用.NET Reflector工具查看这个dll里面的类、方法等。


基本上有了官方源代码例子以及Reflector工具,我们就可以利用它进行编程实现了。使用方式很简单,只要在项目中引用LumiSoft.Net.dll这一个文件即可。下面我就直接贴出一段完整的控制台项目代码,实现的是收取邮件(显示)并下载邮件的附件(支持多个附件)到指定目录下的功能。没有漂亮的界面,留给读者自行完善吧。

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. namespace MailHelper  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             using (POP3_Client pop3 = new POP3_Client())  
  8.             {  
  9.                 pop3.Connect("pop.qq.com", 995, true);  
  10.                 pop3.Login("id""password");//两个参数,前者为Email的账号,后者为Email的密码  
  11.   
  12.                 POP3_ClientMessageCollection messages = pop3.Messages;  
  13.                 Console.WriteLine("共{0}封邮件", messages.Count);  
  14.   
  15.                 for (int i = 0; i < messages.Count; i++)  
  16.                 {  
  17.                     POP3_ClientMessage message = messages[i];//转化为POP3  
  18.                     Console.WriteLine("\r\n正在检查第{0}封邮件...", i + 1);  
  19.                     if (message != null)  
  20.                     {  
  21.                         byte[] messageBytes = message.MessageToByte();  
  22.                         Mail_Message mime_message = Mail_Message.ParseFromByte(messageBytes);  
  23.   
  24.                         string sender = mime_message.From == null ? "sender is null" : mime_message.From[0].DisplayName;  
  25.                         string senderAddress = mime_message.From == null ? "senderAddress is null" : mime_message.From[0].Address;  
  26.                         string subject = mime_message.Subject ?? "subject is null";  
  27.                         string recDate = mime_message.Date == DateTime.MinValue ? "date not specified" : mime_message.Date.ToString();  
  28.                         string content = mime_message.BodyText ?? "content is null";  
  29.                         Console.WriteLine("邮件地址为{0}的{1},于{2}发送了主题为{3}的邮件", senderAddress, sender, recDate, subject);  
  30.                         Console.WriteLine("内容为{0}", content);  
  31.   
  32.                         MIME_Entity[] attachments = mime_message.GetAttachments(truetrue);  
  33.   
  34.                         foreach (MIME_Entity entity in attachments)  
  35.                         {  
  36.                             if (entity.ContentDisposition != null)  
  37.                             {  
  38.                                 string fileName = entity.ContentDisposition.Param_FileName;  
  39.                                 if (!string.IsNullOrEmpty(fileName))  
  40.                                 {  
  41.                                     DirectoryInfo dir = new DirectoryInfo(@"D:\email\");  
  42.                                     if (!dir.Exists) dir.Create();  
  43.   
  44.                                     string path = Path.Combine(dir.FullName, fileName);  
  45.                                     MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body;  
  46.                                     Stream decodedDataStream = byteObj.GetDataStream();  
  47.                                     using (FileStream fs = new FileStream(path, FileMode.Create))  
  48.                                     {  
  49.                                         LumiSoft.Net.Net_Utils.StreamCopy(decodedDataStream, fs, 4000);  
  50.                                     }  
  51.                                     Console.WriteLine("{0}已经被下载。", fileName);  
  52.                                 }  
  53.                             }  
  54.                         }  
  55.                     }  
  56.                 }  
  57.             }  
  58.         }  
  59.     }  
  60. }  
效果图:



0 0
原创粉丝点击