首页 > Windows开发 > 详细

C# 接收邮件

时间:2015-04-25 18:10:34      阅读:319      评论:0      收藏:0      [点我收藏+]
 /// <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);
            }

 

C# 接收邮件

原文:http://www.cnblogs.com/streamice/p/EAGetMail.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!