【转】通过第三方控件收邮件(LumiSoft.Net.POP3.Client)

原文地址:http://www.cnblogs.com/aprillee/archive/2008/02/01/997117.html

    最近我们部门头,让我研究一下收邮件的功能。因为工作忙的原因一直没时间查找。先前也查到这个控件并且把源码和帮助都下载到本地了。就是没时间看。周末终于有时间研究一下了。
下面是下载源码的地址:
下载LumiSoft.Net类库,并下载LumiSoft.Net文档。下载完后用VS2005把源码打开生成release的。然后引入你需要接收邮件的项目。
下面是接收邮件的类文件

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mime;
using System.Collections.Generic;
/// <summary>
/// pop 的摘要说明
/// </summary>
public class pop
{
    public List<Mime> GetEmails()
    {
        //需要首先设置这些信息
        string pop3Server = "";    //邮箱服务器 如:"pop.sina.com.cn";或 "pop.tom.com" 好像sina的比较快
        int pop3Port=110;          //端口号码   用"110"好使。最好看一下你的邮箱服务器用的是什么端口号
        bool pop3UseSsl=false;
        string username="";        //你的邮箱用户名
        string password = "";      //你的邮箱密码
        List<string> gotEmailIds=new List<string>();

        List<Mime> result = new List<Mime>();
        using (POP3_Client pop3 = new POP3_Client())
        {
  try
            {
             //与Pop3服务器建立连接
            pop3.Connect(pop3Server, pop3Port, pop3UseSsl);
            //验证身份
            pop3.Authenticate(username, password, false);

            //获取邮件信息列表
           POP3_ClientMessageCollection infos = pop3.Messages;

           foreach (POP3_ClientMessage info in infos)
            {
                //每封Email会有一个在Pop3服务器范围内唯一的Id,检查这个Id是否存在就可以知道以前有没有接收过这封邮件
                if (gotEmailIds.Contains(info.UID))
                    continue;

                //获取这封邮件的内容
                byte[] bytes = info.MessageToByte();
                //记录这封邮件的Id
                gotEmailIds.Add(info.UID);

                //解析从Pop3服务器发送过来的邮件信息
                Mime mime = Mime.Parse(bytes);

                result.Add(mime);
            }
           }
catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        return result;
    }
    //public void ShowEmail(Mime m)
    //{
    //    Console.WriteLine("From: {0}", m.MainEntity.From.ToAddressListString());

    //    Console.WriteLine("To: {0}", m.MainEntity.To.ToAddressListString());

    //    Console.WrtieLine("Time: {0}", m.MainEntity.Date);            //发送时间

    //    Console.WriteLine("Subject: {0}", m.MainEntity.Subject);      //主题

    //    Console.WriteLine("Plain Body: {0}", m.BodyText);             //内容

    //    Console.WriteLine("Html Body: {0}", m.BodyHtml);              //HTML格式内容
    //}

}


下面是调用上面接收类的
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using LumiSoft.Net.Mime;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        pop p = new pop();
        List<Mime> dd = p.GetEmails();        //dd中就可以查找出邮件的内容、主题、发件人等信息。你可以通过调试状态的快速监视查看
         foreach (Mime mdd in dd)
        {
            Page.Response.Write(mdd.MainEntity.Date + "<br><br>");    //发送时间          Page.Response.Write(mdd.MainEntity.Subject + "<br><br>"); //主题
            Page.Response.Write(mdd.BodyText + "<br><br>");           //内容
        }
                                              //因为时间关系没有写完明天待续。。。
    }
}

posted @ 2010-10-15 10:51  陛下  阅读(1273)  评论(0编辑  收藏  举报