public static List<Email> GetAllEmails(string server, int port, string user, string pwd, bool usessl, bool deleteafterread, ref List<string> errors )
        {
            var ret = new List<Email>();
            errors.Clear();
            var oClient = new POP3_Client();
            try
            {
                oClient.Connect(server, port, usessl);
                oClient.Authenticate(user, pwd, true);
            }
            catch (Exception exception)
            {
                errors.Add("Error connect/authenticate - " + exception.Message);
                return null;
            }
            foreach (POP3_ClientMessage message in oClient.Messages)
            {
                var wrapper = new Email();
                wrapper.Uid = message.UID;
                try
                {
                    Mail_Message mime = Mail_Message.ParseFromByte(message.HeaderToByte());
                    wrapper.Subject = mime.Subject;
                    wrapper.From = mime.From[0].Address;
                    wrapper.To = mime.To[0].ToString();
                    mime = Mail_Message.ParseFromByte(message.MessageToByte());

                    string sa = mime.BodyHtmlText;
                    if (sa == null)
                        wrapper.TextBody = mime.BodyText;
                    else
                        wrapper.HtmlBody = sa;
                }
                catch (Exception exception)
                {
                    errors.Add("Error reading " + wrapper.Uid + " - " + exception.Message);
                }
                if(deleteafterread)
                    message.MarkForDeletion();
                ret.Add(wrapper);
            }
            oClient.Disconnect();
            oClient.Dispose();

            return ret;
        }
示例#2
0
        /// <summary>
        /// 从邮件中获取新的更新信息
        /// </summary>
        protected void GetEmailUpdateInfo()
        {
            //获取邮件配置信息
            FrameConfig FC = new FrameConfig();
            string ConfigStr = FC.GetDetail("HostMail").ConfigValue;
            FrameCommon FComm = new FrameCommon();
            string UserName = FComm.FindValueFromStr(ConfigStr, "UserName="******"Password="******"POP3Host=").Trim();
            int Pop3Port = Convert.ToInt32(FComm.FindValueFromStr(ConfigStr, "POP3Port=").Trim());
            bool IsSSL = FComm.FindValueFromStr(ConfigStr, "EnableSsl=").Trim() == "1" ? true : false;
            //连接邮件服务器
            using (POP3_Client MyClient = new POP3_Client())
            {
                MyClient.Connect(Pop3Host, Pop3Port, IsSSL);
                MyClient.Authenticate(UserName, Password, false);
                string SystemGuid = FComm.GetSystemGuid();
                for (int i = 0; i < MyClient.Messages.Count; i++)
                {
                    POP3_ClientMessage mail = MyClient.Messages[i];
                    Mime m = Mime.Parse(mail.MessageToByte());
                    if (m.Attachments.Length > 0 && m.MainEntity.Subject.ToLower().IndexOf(SystemGuid.ToLower()) > -1) //如果有附件,且为该系统的标识
                    {
                        string UpdateSubject = m.MainEntity.Subject;
                        DateTime UpdateTime = m.MainEntity.Date;
                        string UpdateContent = m.BodyText;
                        int FileCount = m.Attachments.Length;
                        string UpdateGuid = System.Guid.NewGuid().ToString();
                        string FilePath = "/UpdateBackUp/" + UpdateTime.ToString("yyyyMMdd") + "-" + new System.Random().Next(1000, 9999).ToString() + "/"; //生成路径
                        string FilenameList = "";
                        //将基本信息存入数据库

                        foreach (MimeEntity entry in m.Attachments)
                        {

                            string fileName = entry.ContentDisposition_FileName; //获取文件名称
                            if (fileName != null && fileName.Trim() != "")  //有些文件取不到,如txt
                            {
                                FilenameList = FilenameList + fileName + ";";
                                string DirPath = Server.MapPath(FilePath);
                                System.IO.Directory.CreateDirectory(DirPath);
                                string FileURL = DirPath + fileName;
                                FileInfo fi = new FileInfo(FileURL);
                                byte[] data = entry.Data;
                                FileStream pFileStream = null;
                                pFileStream = new FileStream(FileURL, FileMode.Create);
                                pFileStream.Write(data, 0, data.Length);
                                pFileStream.Close();
                            }
                        }
                        mail.MarkForDeletion();
                        new FrameUpdate().InsertUpdate(UpdateGuid, UpdateTime, UpdateSubject, UpdateContent, FileCount, FilenameList, FilePath);
                    }

                }
                MyClient.Disconnect();
            }

            this.Refresh();
        }