[置顶] jmail + c#实现邮件的接收

   

   引言


   在上一篇博客中和大家分享了关于邮件发送的学习,既然邮件实现了发送,那么我们仍然需要实现邮件接收的


功能,那么下面就和大家分享一下关于邮件发送的基本实现过程,当然我们在实现的过程中仍然利用jmail插件来实


现,和广大读者分享代码:


  前台代码:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        邮件服务器:<asp:TextBox ID="TextBox_popServer" runat="server"></asp:TextBox>
    
        <br />
        邮箱账户:<asp:TextBox ID="TextBox_userID" runat="server"></asp:TextBox>
        <br />
        邮箱密码:<asp:TextBox ID="TextBox_paw" runat="server"></asp:TextBox>
        <br />
        端口:<asp:TextBox ID="TextBox_port" runat="server"></asp:TextBox>
        <br />
        <asp:CheckBox ID="CheckBox_ssl" runat="server" Text="SSL登陆" />
        <br />
        <asp:Button ID="Button_Receive" runat="server" Text="收邮件" 
            onclick="Button_Receive_Click" />
        <br />
    <hr />
        <asp:Label ID="Label_mail" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

   后台代码:

using System;
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 jmail;
using System.IO;
using System.Text;

namespace youjianceshi
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //*****************************解决标题乱码**********************************
        public string DecodeStr(string str)
        {
            string result = "";
            if (str != "" || str != null)
            {
                if (str.ToUpper().Contains("UTF-8"))
                {
                    String[] array = str.Split('?');
                    if (array.Length > 2)
                    {
                        string title = array[3];
                        byte[] bytes = Convert.FromBase64CharArray(title.ToCharArray(), 0, title.ToCharArray().Length);
                        Encoding en = Encoding.GetEncoding("utf-8");
                        result = en.GetString(bytes);
                    }

                }
            }
            return result;
        } 

        //***************************************************************************
        //private void ReciveByJmail()
        //{
        //    /**/
        //    ///建立收邮件对象   
        //    jmail.POP3Class popMail = new POP3Class();

        //    /**/
        //    ///建立邮件信息接口   
        //    jmail.Message mailMessage;

        //    /**/
        //    ///建立附件集接口   
        //    jmail.Attachments atts;

        //    /**/
        //    ///建立附件接口   
        //    jmail.Attachment att;

        //    //try
        //    //{
        //    popMail.Connect(this.TextBox_userID.Text, this.TextBox_paw.Text, this.TextBox_popServer.Text, 110);

        //    /**/
        //    ///如果收到邮件   
        //    if (0 < popMail.Count)
        //    {
        //        /**/
        //        ///根据取到的邮件数量依次取得每封邮件   
        //        for (int i = 1; i <= popMail.Count; i++)
        //        {
        //            /**/
        //            ///取得一条邮件信息   
        //            mailMessage = popMail.Messages[i];

        //            /**/
        //            ///取得该邮件的附件集合   
        //            atts = mailMessage.Attachments;

        //            /**/
        //            ///设置邮件的编码方式                             
        //            mailMessage.Charset = "gb2312";

        //            /**/
        //            ///设置邮件的附件编码方式                            
        //            mailMessage.Encoding = "Base64";

        //            /**/
        //            ///是否将信头编码成iso-8859-1字符集                           
        //            mailMessage.ISOEncodeHeaders = true;

        //            mailMessage.ContentType = "text/html";




        //            /**/
        //            ///邮件的发送人的信箱地址                         
        //            string _fromMail = mailMessage.From;

        //            /**/
        //            ///邮件的发送人                        
        //            string _fromName = mailMessage.FromName;



        //            /**/
        //            ///邮件主题                      
        //            string _subject = mailMessage.Subject;

        //            /**/
        //            ///邮件内容                      
        //            string _body = mailMessage.HTMLBody;

        //            this.Label_mail.Text += "主题:" + _subject + "<br>";
        //            this.Label_mail.Text += "发件人:" + _fromName + "<br>";
        //            this.Label_mail.Text += "内容:" + _body + "<hr>";


        //        }
        //        att = null;
        //        atts = null;
        //    }
        //    else
        //    {
        //        Response.Write("没有新邮件!");
        //    }

        //    //popMail.DeleteMessages();

        //    popMail.Disconnect();
        //    popMail = null;
        //    //}
        //    //catch
        //    //{
        //    //    Response.Write("Warning!请检查邮件服务器的设置是否正确!");
        //    //}
        //}
        protected void Button_Receive_Click(object sender, EventArgs e)
        {
            this.Label_mail.Text = "";

            reciveMail();

        }
        private void reciveMail()
        {

            string poptity, senders, sendmail, subject, HtmlBody, TextBody, date, size, path;
            string mailname = this.TextBox_userID.Text;
            string mailpwd = this.TextBox_paw.Text;
            string pop = this.TextBox_popServer.Text;

            
            jmail.POP3Class popMail = new POP3Class();
            jmail.Message mailMessage;
            jmail.Attachments atts;
            jmail.Attachment att;


            //popMail.Connect(mailname, mailpwd, pop, 110);
            popMail.Connect(mailname, mailpwd, pop, 110);
            if (0 < popMail.Count)
                //int i = 1; i <= popMail.Count; i++
            {
                for (int i = popMail.Count; i >= popMail.Count-10; i--)
                {
                    mailMessage = popMail.Messages[i];
                    atts = mailMessage.Attachments;
                    mailMessage.Charset = "UTF8";
                    //mailMessage.Charset = "gb2312";
                    
                    mailMessage.Encoding = "Base64";
                    mailMessage.ISOEncodeHeaders = false;
                    poptity = mailMessage.Priority.ToString();
                    senders = mailMessage.FromName;
                    sendmail = mailMessage.From.ToString();
                    subject = mailMessage.Subject;
                    HtmlBody = mailMessage.HTMLBody;
                    TextBody = mailMessage.Body;
                    date = mailMessage.Date.ToString();
                    size = mailMessage.Size.ToString();

                   string str= mailMessage.Headers.GetHeader("Subject");
                    //string[] strss = str.Split('?');
                    //byte[] bytystr = Convert.FromBase64String(strss[50]);

                    //string strlast = Encoding.Default.GetString(bytystr);
                   

                    string attNameList = "";

                    #region 收取附件


                    for (int j = 0; j < atts.Count; j++)
                    {
                        if (j == 0)
                        {
                            //取得附件名称
                            att = atts[j];
                            //附件名称
                            string attname = att.Name;

                            attNameList += attname + " | ";

                            string UploadFileLastName = attname.Substring(attname.LastIndexOf(".") + 1);//得到文件的扩展名
                            Random rd = new Random();//产生随机数
                            int valationNo = 10 + rd.Next(99);//产生随机数
                            string suiji = valationNo.ToString();//产生随机数

                            string UpLoadFileTime = DateTime.Now.ToString("yyyyMMddHHmmss") + suiji;//得到系统时间并加上随机数以便生成上传图片名称
                            string _newUploadFileName = UpLoadFileTime + "." + UploadFileLastName;//产生上传图片的名称

                            string _userDirectory = DateTime.Now.ToString("yyyyMMdd");//所要创建文件夹的名字,实际运用中可为用户注册ID

                            string filePath = Server.MapPath("/Attachments/") + _userDirectory;

                            if (!Directory.Exists(filePath)) //如果文件夹不存在则创建
                            {
                                Directory.CreateDirectory(filePath);
                            }

                            string _files = filePath + "\\" + _newUploadFileName;
                            att.SaveToFile(_files);


                        }



                    }

                    #endregion


                    //popMail.DeleteSingleMessage(i);



                    this.Label_mail.Text += "主题:" + subject + "<br>";
                    this.Label_mail.Text += "发件人:" + senders + "<" + sendmail + "><br>";
                    this.Label_mail.Text += "发送时间:" + date + "<br>";
                    this.Label_mail.Text += "邮件大小:" + size + "<br>";
                    this.Label_mail.Text += "邮件优先级:" + poptity + "<br>";
                    this.Label_mail.Text += "附件:" + attNameList + "<br>";
                    this.Label_mail.Text += "内容:<br>" + HtmlBody + "<hr>";


                }
                att = null;
                atts = null;
                popMail.Disconnect();
                popMail = null;
            }
            else
            {
                att = null;
                atts = null;
                popMail.Disconnect();
                popMail = null;
            }
        }
    }
}

  从上面的代码中给出了两种接收邮件的方式,读者可以在实现时候尝试一下这两种不同的实现方式有什么不同!


对于jmail的操作和发送邮件的时候一样,读者们可以自行参考上一篇博客。在代码中小编没有将所有的邮件都收


取,而是收取了最新的几十封邮件,因为收取所有的邮件花费的时间非常的长,如果读者有需要,可以自行修改代


码。


   小结


   经过这几天的学习和研究对于jmail这个插件有所了解,所以说我们在解决问题的时候总是能学习到很多的知


识,解决问题的时候就是学习的时候,虽然实现了这个功能,但是并不是非常的完美,所以说在接下来的时间了,小


编会用另外一种思路来解决收发邮件的问题,如果有了新的想法并且实现了,小编会继续和大家分享!!

你可能感兴趣的:(C#,jmail,收发邮件)