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这一个文件即可。下面我就直接贴出一段完整的控制台项目代码,实现的是收取邮件(显示)并下载邮件的附件(支持多个附件)到指定目录下的功能。没有漂亮的界面,留给读者自行完善吧。
- namespace MailHelper
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (POP3_Client pop3 = new POP3_Client())
- {
- pop3.Connect("pop.qq.com", 995, true);
- pop3.Login("id", "password");//两个参数,前者为Email的账号,后者为Email的密码
- POP3_ClientMessageCollection messages = pop3.Messages;
- Console.WriteLine("共{0}封邮件", messages.Count);
- for (int i = 0; i < messages.Count; i++)
- {
- POP3_ClientMessage message = messages[i];//转化为POP3
- Console.WriteLine("\r\n正在检查第{0}封邮件...", i + 1);
- if (message != null)
- {
- byte[] messageBytes = message.MessageToByte();
- Mail_Message mime_message = Mail_Message.ParseFromByte(messageBytes);
- string sender = mime_message.From == null ? "sender is null" : mime_message.From[0].DisplayName;
- string senderAddress = mime_message.From == null ? "senderAddress is null" : mime_message.From[0].Address;
- string subject = mime_message.Subject ?? "subject is null";
- string recDate = mime_message.Date == DateTime.MinValue ? "date not specified" : mime_message.Date.ToString();
- string content = mime_message.BodyText ?? "content is null";
- Console.WriteLine("邮件地址为{0}的{1},于{2}发送了主题为{3}的邮件", senderAddress, sender, recDate, subject);
- Console.WriteLine("内容为{0}", content);
- MIME_Entity[] attachments = mime_message.GetAttachments(true, true);
- foreach (MIME_Entity entity in attachments)
- {
- if (entity.ContentDisposition != null)
- {
- string fileName = entity.ContentDisposition.Param_FileName;
- if (!string.IsNullOrEmpty(fileName))
- {
- DirectoryInfo dir = new DirectoryInfo(@"D:\email\");
- if (!dir.Exists) dir.Create();
- string path = Path.Combine(dir.FullName, fileName);
- MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body;
- Stream decodedDataStream = byteObj.GetDataStream();
- using (FileStream fs = new FileStream(path, FileMode.Create))
- {
- LumiSoft.Net.Net_Utils.StreamCopy(decodedDataStream, fs, 4000);
- }
- Console.WriteLine("{0}已经被下载。", fileName);
- }
- }
- }
- }
- }
- }
- }
- }
- }
0 0
- LumiSoft收取邮件(含邮件附件)
- LumiSoft收取邮件(含邮件附件)
- 使用LumiSoft.Net收取邮件(续)
- 使用LumiSoft.Net收取邮件
- 邮件收取客户端LumiSoft类库接收yahoo邮件的问题。
- asp 收取邮件(完整版)
- .net jMail收邮件(含写入数据库及收取附件)
- 用Jmail做收取邮件附件的程序
- C# jmail收取邮件(带附件)
- JAVAMAIL收取邮件
- pop3收取邮件过程
- Pop3收取邮件
- Foxmail收取邮件
- php 收取邮件
- pop3协议收取邮件
- 189邮件收取
- POP3收取邮件
- powershell pop3 收取邮件
- 黑马程序员—离开黑马,一切都并未结束,我们的故事,未完待续--------
- MYSQL存储过程
- taskkill命令的使用
- 单链表逆序或者逆序输出
- EXT JS 开发问题集锦
- LumiSoft收取邮件(含邮件附件)
- 黑马程序员—马上快毕业了,给你们点建议吧
- Objective C运行时(runtime)技术的几个要点总结
- LeetCode 54 Jump Game
- 常用SQL整理2
- wpa_supplicant无线网络配置
- POJ 1845 Sumdiv
- Android开发(五) 如何解决"the currently displayed page contains invalid values"错误
- IOS开发笔记(5)程序异常crash捕获与拦截