pop3

时间:2022-11-23 03:16:43
POP3_Client pop3 = new POP3_Client();
pop3.Connect("mail.fetag.net", , false);
pop3.Authenticate("username", "password", false);
POP3_ClientMessageCollection cmc = pop3.Messages;
foreach (POP3_ClientMessage cm in cmc)
{
byte[] bytes = cm.MessageToByte();
 
Mail_Message m = Mail_Message.ParseFromByte(bytes);
string Subject = m.Subject;
foreach (MIME_Entity entry in m.Attachments)
{
string FileName = entry.ContentDisposition.Param_FileName;
byte[] data = ((MIME_b_SinglepartBase)entry.Body).Data;
FileStream fs = null;
fs = new FileStream(@"d:\" + FileName, FileMode.Create);
fs.Write(data, , data.Length);
fs.Close();
}
} ///////////////
下面是接收邮件的类文件

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>");           //内容
        }
                                              //因为时间关系没有写完明天待续。。。
    }
}