招标系统-如何收邮件(小试牛刀)

本菜在系统验收的过程中,公司提出了收发邮件的需求。经过一番折腾,终于出了一点小小成果。


以下是收邮件实现的代码。


首先引用jmail插件。下载地址  http://download.csdn.net/detail/u013045437/9270547



添加引用

前台页面代码:

<span style="font-size:18px;">邮件服务器:<input class="easyui-textbox" ID="TextBox_popServer" />       
        邮箱账户:<input class="easyui-textbox" ID="TextBox_userID" />       
        邮箱密码:<input class="easyui-textbox" id="Password" />    
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" οnclick="Reseive()">接受邮件</a>   
        <input class="easyui-Label" id="Label_mail" οnkeydοwn="javascript:Enter();"></span>

JS代码:

<span style="font-size:18px;">function doSearch() {
    if ($('#BidProjectId').val() == '') {
        $.messager.alert('提示消息', '请输入招标编号!', 'warning');
        $('#BidProjectId').select();
    } else {
        //显示主体内容页
        document.getElementById('ContentAreas').style.visibility = '';
        var condition = document.getElementById("BidProjectId").value;

        $('#dg').datagrid({
            url: '/Specialist/QuerySpecialistByCondition?condition=' + condition
        });

        $("#dg").datagrid("reload");
    }
}

function Reseive() {
    var popServer = document.getElementById("TextBox_popServer").value;
    var userID = document.getElementById("TextBox_userID").value;
    var password = document.getElementById("Password").value;

    $.post("/Specialist/SpecialistManager",
           {
               "popServer": popServer, "userID": userID, "password": password
           }, function (result) {                      
               
               for (var i = 0; i < result.length ; i++) {
                   Mail="<div>"
                   for (var j = 0; j < result[i].length; j++) {
                       document.getElementById("Label_mail").value += "主题:" + result[i][0] + "<br>";
                       document.getElementById("Label_mail").value += "发件人:" + result[i][2] + "<" + result[i][3] + "><br>";
                       document.getElementById("Label_mail").value += "发送时间:" + result[i][4] + "<br>";
                       document.getElementById("Label_mail").value += "邮件大小:" + result[i][5] + "<br>";
                       document.getElementById("Label_mail").value += "邮件优先级:" + result[i][6] + "<br>";
                       document.getElementById("Label_mail").value += "附件:" + result[i][7] + "<br>";
                       document.getElementById("Label_mail").value += "内容:<br>" + result[i][8] + "<hr>";
                       Mail
                   }
                   Mail
               }
           });
}</span>

后代代码( Controller):

<span style="font-size:18px;">public JsonResult receiveMail()
        {
            StringBuilder myMail = new StringBuilder();
            StringBuilder MailText = new StringBuilder();
            string poptity, senders, sendmail, subject, HtmlBody, TextBody, date, size, path;        
            string mailname = Request["userID"];
            string mailpwd = Request["password"];
            string pop = Request["popServer"];


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

            IList<IList<string>> Eemail = new List<IList<string>>();

            //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 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

                   
                    List<string> content = new List<string>();
                    content.Add(subject);
                    content.Add(senders);
                    content.Add(sendmail);
                    content.Add(date);
                    content.Add(size);
                    content.Add(poptity);
                    content.Add(attNameList);
                    content.Add(HtmlBody);

                    Eemail.Add(content.ToList());

                }
                att = null;
                atts = null;
                popMail.Disconnect();
                popMail = null;
            }
            else
            {
                att = null;
                atts = null;
                popMail.Disconnect();
                popMail = null;
            }
            //var data = new
            //{
            //    Length = Eemail.Count,
            //    content = Eemail
            //};
            return Json(Eemail, JsonRequestBehavior.AllowGet);

        }  
    }</span>

          此次实现收邮件的功能,只是小弑牛刀,也借此机会分享,如果系统中有需要实现此功能的,敬请读者参考指正。


                                            参考博客: Jamil+C#——实现邮件的接收

                                            发送邮件更多功能敬请点击: Jamil+C#——实现邮件的发送

                                                           


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值