C# 接收邮件

 /// <summary>
    /// 收取新邮件、不删除老邮件、收取邮件后写入数据库
    /// </summary>
    public static void GetNewMailIntoDataBase(string UserName,string PassWord,string PopServer,int Port,DateTime MaxDate)
    {

        string curpath = Directory.GetCurrentDirectory();
        string mailbox = String.Format("{0}\\inbox", curpath);

        // If the folder is not existed, create it.
        if (!Directory.Exists(mailbox))
        {
            Directory.CreateDirectory(mailbox);
        }

        MailServer oServer = new MailServer(PopServer,
                    UserName, PassWord, ServerProtocol.Pop3);
        MailClient oClient = new MailClient("TryIt");

        // Enable SSL connection.
        oServer.SSLConnection = true;

        // Set 995 SSL port
        oServer.Port = 995;
         try
            {
                oClient.Connect(oServer);
                MailInfo[] infos = oClient.GetMailInfos();
                for (int i = 0; i < infos.Length; i++)
                {
                    MailInfo info = infos[i];
                    Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
                        info.Index, info.Size, info.UIDL);

                    System.DateTime d = System.DateTime.Now;
                    System.Globalization.CultureInfo cur = new
                       System.Globalization.CultureInfo("en-US");
                    string sdate = d.ToString("yyyyMMddHHmmss", cur);
                    // Receive email from POP3 server
                    Mail oMail = oClient.GetMail(info);
                    
                    ZWL.BLL.ERPNetEmail MyModel = new ZWL.BLL.ERPNetEmail();
                    MyModel.EmailContent = "";
                    MyModel.EmailState = "未读";
                    MyModel.EmailContent = oMail.HtmlBody;
                    MyModel.EmailTitle = oMail.Subject;
                    MyModel.FromUser = oMail.From.ToString();
                    foreach (EAGetMail.Attachment attrachment in oMail.Attachments)
                    {
                        string filefoot = attrachment.Name.Substring(attrachment.Name.LastIndexOf(".") + 1, (attrachment.Name.Length - attrachment.Name.LastIndexOf(".") - 1));   //扩展名
                        switch (filefoot)
                        {
                            case "doc": MyModel.FuJian = sdate + ".doc"; break;
                            case "docx": MyModel.FuJian = sdate + ".docx"; break;
                            case "xls": MyModel.FuJian = sdate + ".xls"; break;
                            case "xlsx": MyModel.FuJian = sdate + ".xlsx"; break;
                            case "ppt": MyModel.FuJian = sdate + ".ppt"; break;
                            case "pptx": MyModel.FuJian = sdate + ".pptx"; break;
                            case "rar": MyModel.FuJian = sdate + ".rar"; break;
                            case "zip": MyModel.FuJian = sdate + ".zip"; break;
                            case "txt": MyModel.FuJian = sdate + ".txt"; break;
                            case "jpg": MyModel.FuJian = sdate + ".jpg"; break;
                            case "png": MyModel.FuJian = sdate + ".png"; break;
                        }
                        //if (attrachment.Name.Contains(".doc")||attrachment.Name.Contains(".docx"))
                        //{
                        //    MyModel.FuJian = sdate + ".doc";
                        //}
                        //if (attrachment.Name.Contains(".xls") || attrachment.Name.Contains(".xlsx"))
                        //{
                        //    MyModel.FuJian = sdate + ".xls";
                        //}
                        //if (attrachment.Name.Contains(".ppt") || attrachment.Name.Contains(".pptx"))
                        //{
                        //    MyModel.FuJian = sdate + ".ppt";
                        //}
                        //if (attrachment.Name.Contains(".rar"))
                        //{
                        //    MyModel.FuJian = sdate + ".rar";
                        //}
                        //if (attrachment.Name.Contains(".zip"))
                        //{
                        //    MyModel.FuJian = sdate + ".zip";
                        //}
                        //if (attrachment.Name.Contains(".txt"))
                        //{
                        //    MyModel.FuJian = sdate + ".txt";
                        //}
                        string fileName1 = System.Web.HttpContext.Current.Request.MapPath("../UploadFile") + "\\" + MyModel.FuJian;
                        attrachment.SaveAs(fileName1, true);
  
                    }
                    MyModel.ToUser = ZWL.Common.PublicMethod.GetSessionValue("UserName");
                    MyModel.TimeStr = oMail.ReceivedDate;
                    MyModel.Add();
                    Console.WriteLine("From: {0}", oMail.From.ToString());
                    Console.WriteLine("Subject: {0}\r\n", oMail.Subject);

         

                    string fileName = System.Web.HttpContext.Current.Request.MapPath("../UploadFile") + "\\" + MyModel.FuJian;

                    // Save email to local disk

                  //  oMail.SaveAs(fileName, true);

                    // Mark email as deleted from POP3 server.
                    oClient.Delete(info);
                }

                // Quit and pure emails marked as deleted from POP3 server.
                oClient.Quit();
            }
            catch (Exception ep)
            {
                Console.WriteLine(ep.Message);
            }

 

posted @ 2015-04-25 17:04  streamice  阅读(2310)  评论(0编辑  收藏  举报