博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pop3
阅读量:7221 次
发布时间:2019-06-29

本文共 3242 字,大约阅读时间需要 10 分钟。

POP3_Client pop3 = new POP3_Client();pop3.Connect("mail.fetag.net", 110, 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, 0, 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>");           //内容
        }
                                              //因为时间关系没有写完明天待续。。。
    }
}

转载地址:http://xixym.baihongyu.com/

你可能感兴趣的文章
rn.ShowDialog() == DialogResult.OK
查看>>
20160519
查看>>
SCU 3132(博弈)
查看>>
正则表达式
查看>>
delete archivelog all 无法彻底删除归档日志?
查看>>
Redis五大数据类型
查看>>
大型分布式网站架构技术总结
查看>>
矩阵求导与投影梯度相关问题
查看>>
SVN
查看>>
C语言编程写的一个http下载程序(王德仙)2012-04-08
查看>>
CCF201409-3 字符串匹配(100分)
查看>>
UVALive2203 UVa10042 Smith Numbers【质因数分解+素数判定+数位之和】
查看>>
Project Euler Problem 9: Special Pythagorean triplet
查看>>
HDU5701 中位数计数【中位数】
查看>>
Python 深浅拷贝 (Shallow copy and Deep copy in Python)
查看>>
Axure
查看>>
屏幕截取工具
查看>>
C语言第七次作业---要死了----
查看>>
Jquery事件绑定冲突
查看>>
偶现bug如何处理?
查看>>