邮件接收

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mime;
using System.Collections.Generic;
using System.Text;
using System.IO; 

namespace WebTest
{
    public partial class Pop3Test : System.Web.UI.Page
    {
        //public Emails ListEmail
        //{
        //    get
        //    {
        //        if (ViewState["listEmail"] == null)
        //            throw new ApplicationException();
        //        return (Emails)ViewState["listEmail"];
        //    }
        //    set { ViewState["listEmail"] = value; }
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                Emails emais = new Emails(GetEmails());
                Session["ListEmail"] = emais;
                lblCount.Text = "共" + emais.ListEmailContent .Count.ToString() + "封邮件";
            }
            catch (Exception)
            {
                lblCount.Text = "连接失败!";
            }
        }

        protected void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                List<Mime> listEmails = ((Emails)Session["ListEmail"]).ListEmailContent;
                int num = Convert.ToInt32(txtBh.Text.Trim()) - 1;
                //邮件内容
                //divContent1.InnerHtml = listEmails[num].BodyText;
                //divContent2.InnerHtml = listEmails[num].BodyHtml;

                //邮件表头信息
                //divContent1.InnerHtml = "From" + DecodeMailto(listEmails[num].MainEntity.From.ToAddressListString()) + "To" + listEmails[num].MainEntity.To.ToAddressListString() + "Time" + listEmails[num].MainEntity.Date + "主题" + listEmails[num].MainEntity.Subject;
                //divContent2.InnerHtml = "From" + DecodeMailto(listEmails[num].MainEntity.From.ToAddressListString()) + "To" + listEmails[num].MainEntity.To.ToAddressListString() + "Time" + listEmails[num].MainEntity.Date + "主题" + listEmails[num].MainEntity.Subject;

                //邮件附件
                foreach (MimeEntity me in listEmails[num].Attachments)
                {
                    //附件文件名
                    string attname = me.ContentDisposition_FileName;//附件名称
                    //附件大小
                    string attsize = ((int)(me.Data.Length / 1000) == 0 ? 1 : (int)(me.Data.Length / 1024)).ToString() + "KB";
                    //if (!File.Exists(Server.MapPath("AttachFiles") + "//" + "FromUser" + "//" + attname))
                    //{
                    //    //附件存储到服务器
                    //    FileStream fs = new FileStream(Server.MapPath("AttachFiles") + "//" + "FromUser" + "//" + attname, FileMode.Create);
                    //    fs.Write(me.Data, 0, me.Data.Length); //将字节数组存储到文件流中
                    //    fs.Close();
                    //}

                    if (!File.Exists(Server.MapPath("AttachFiles") + "//" + "FromUser"))
                    {

                    }
                    //附件存储到服务器
                    FileStream fs = new FileStream(Server.MapPath("AttachFiles") + "//" + "FromUser" + "//" + attname, FileMode.Create);
                    fs.Write(me.Data, 0, me.Data.Length); //将字节数组存储到文件流中
                    fs.Close();
                }
            }
            catch (Exception)
            {
                divContent1.InnerHtml = "获取邮件失败!";
            }
        }

        #region 邮件列表
        public List<Mime> GetEmails()
        {
            //需要首先设置这些信息  
            string pop3Server = txtServer .Text .Trim ();    //邮箱服务器 如:"pop.sina.com.cn";或 "pop.tom.com" 好像sina的比较快  
            int pop3Port = 110;          //端口号码   用"110"好使。最好看一下你的邮箱服务器用的是什么端口号  
            bool pop3UseSsl = false;
            string username = txtUser .Text .Trim ();        //你的邮箱用户名  
            string password = txtPwd.Text .Trim ();      //你的邮箱密码  
            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;
        }  
        #endregion

        #region  UTF8解码
        public string DecodeMailto(string StrSource)
        {
            try
            {
                string[] strarr = StrSource.Split('?');
                byte[] bytes = Convert.FromBase64String(strarr[3]);
                return Encoding.UTF8.GetString(bytes);
            }
            catch
            {
                return StrSource;
            }
        }

        #endregion
    }
}

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Pop3Test.aspx.cs" Inherits="WebTest.Pop3Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>服务器</td>
    <td><asp:TextBox runat ="server" ID="txtServer">pop3.163.com</asp:TextBox></td>
    </tr>
    <tr>
    <td>用户名</td>
    <td><asp:TextBox runat ="server" ID="txtUser">hbwangcaihua@163.com</asp:TextBox></td>
    </tr>
    <tr>
    <td>密码</td>
    <td><asp:TextBox runat ="server" ID="txtPwd" Text ="881811881811"></asp:TextBox></td>
    </tr>
    <tr>
    <td><asp:Button runat ="server" ID="btnConnect" Text ="连接"
            οnclick="btnConnect_Click"/></td>
    <td><asp:Label runat ="server" ID="lblCount"></asp:Label></td>
    </tr>
    <tr>
    <td>输入邮件编号<asp:TextBox runat ="server" ID="txtBh" Width="26px"></asp:TextBox></td>
    <td><asp:Button runat ="server" ID="btnOpen" Text ="接收" οnclick="btnOpen_Click" /></td>
    </tr>
    <tr>
    <td colspan ="2">
    BodyText<br />
    <div runat ="server" id="divContent1"></div>
    </td>
    </tr>
    <tr>
    <td colspan ="2">
    BodyInnerHtml<br />
    <div runat ="server" id="divContent2"></div>
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using LumiSoft.Net.Mime;

namespace WebTest
{
    public class Emails
    {
        private List<Mime> listEmailContent = new List<Mime>();
        public List<Mime> ListEmailContent
        {
            get { return listEmailContent; }
            set { listEmailContent = value; }
        }

        public Emails()
        { }

        public Emails(List<Mime> list)
        {
            listEmailContent = list;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值