Exemplo n.º 1
0
 private void connectBtn_Click(object sender, EventArgs e)
 {
     new Task(() =>
     {
     });
     _emailManager.Connect_Click(a =>
     {
         connectStatusTBox.Text = a;
     });
 }
Exemplo n.º 2
0
        ///// <summary>
        ///// 运行时要读昨天的邮件(之后不匹配)
        ///// </summary>
        //private void ReadEmailAtYesterday()
        //{
        //    _emailManager.Connect_Click();
        //    for (int i = _emailManager.MessageCount; i > 0; i--)
        //    {
        //        var email = _emailManager.Retrieve_Click(i);
        //        if (email != null && email.Date != null )
        //        {
        //            if(email.Date >= _lastListenTime.AddDays(-1))
        //            {
        //                _emailAtToday.Add(email);
        //            }else
        //            {
        //                break;
        //            }
        //        }else
        //        {
        //            throw new Exception("监听邮件失败,ReadEmailAtYesterday报错");
        //        }
        //    }
        //    _emailManager.Disconnect_Click();
        //}
        #region 存在漏洞,有时MessageCount比较大时,邮件时间反而小--benjamin20200413
        public void StartThread(object ps)
        {
            PFEmail email = null;

            while (_running == true)
            {
                try
                {
                    //try
                    //{
                    //    //为了检查26服务器上监听邮件失效的问题--benjamin
                    //    PFDataHelper.WriteLocalTxt(string.Format("监听邮件的while进行中,时间:{0}", DateTime.Now.ToString()), "PFListenEmailTask_while.txt");
                    //}
                    //catch (Exception) { }

                    //if (PFDataHelper.IsDebug)
                    //{
                    //    PFDataHelper.WriteLog("1");
                    //}
                    _emailManager.Connect_Click();
                    if (!_emailManager.IsConnect())  //Connect_Click的new TcpClient(_hostName, 110) 是有可能连接失败的--benjamin20200318
                    {
                        Thread.Sleep(1000 * 60 * 5); //如果连接失败,等5分钟
                        continue;
                    }

                    try
                    {
                        //为了检查26服务器上监听邮件失效的问题--benjamin todo
                        PFDataHelper.WriteLocalTxt(string.Format("监听邮件的while的_emailManager.Connect_Click()执行成功,时间:{0},邮件数:{1}", DateTime.Now.ToString(), _emailManager.MessageCount), "PFListenEmailTask_while_Connect_Click.txt");
                    }
                    catch (Exception) { }

                    DateTime?newestMailTime = null; //记录最新邮件的时间
                                                    //bool hasMatch = false;
                                                    //PFDataHelper.WriteLog("MessageCount:" + _emailManager.MessageCount);
                                                    //if (PFDataHelper.IsDebug)
                                                    //{
                                                    //    PFDataHelper.WriteLog("2");
                                                    //    //PFDataHelper.WriteLocalTxt(string.Format("时间{0}\r\nMessageCount:{1}",DateTime.Now, _emailManager.MessageCount), "listenEmail_Connect_Click.txt");
                                                    //}
                    bool isAnyReceiveFail = false;
                    for (int i = _emailManager.MessageCount; i > 0; i--)
                    {
                        //if (PFDataHelper.IsDebug)
                        //{
                        //    PFDataHelper.WriteLog("3");
                        //}
                        email = _emailManager.Retrieve_Click(i);//System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. --benjamin

                        try
                        {
                            //为了检查26服务器上监听邮件失效的问题--benjamin todo
                            PFDataHelper.WriteLocalTxt(string.Format("监听邮件的while的_emailManager.Retrieve_Click()执行成功,时间:{0},邮件数:{1}", DateTime.Now.ToString(), _emailManager.MessageCount), "PFListenEmailTask_while_Retrieve_Click.txt");
                        }
                        catch (Exception) { }

                        //if (PFDataHelper.IsDebug)
                        //{
                        //    PFDataHelper.WriteLog("4");
                        //}
                        if (email == null)
                        {
                            //为了解决这个漏洞:
                            //当第一个邮件能收到,那newestMailTime变成它的时间,但后面的都接收失败了,那么下次就会永远读不到那个失败的邮件
                            newestMailTime   = null;
                            isAnyReceiveFail = true;
                            continue;
                        }//这里不用break是防止id有中间非连续的情况--benjamin20190929

                        if (i == _emailManager.MessageCount)
                        {
                            newestMailTime = email.Date;
                        }

                        //if (email.Date <= _lastListenTime)
                        //if (email.Date != null && _lastListenTime != null &&
                        //    email.Date.Value.AddMinutes(2) < _lastListenTime) //这里用等号似乎会有问题,有时生产者发的邮件时间反而大于消费者回复的邮件的时间(可能邮件上的时间是根据发送端的电脑时间来的,有误差),可能有更好的方法可以统一时间?--benjamin todo
                        //为解决邮件id大但邮件时间反而小的问题
                        if (email.Date != null)
                        {
                            if (email.Date <= _lastListenTime.AddDays(-1)) //昨天的邮件直接不读
                            {
                                email.Dispose();
                                break;
                            }
                            else if (email.Date <= _lastListenTime)          //今天的邮件
                            {
                                if (_emailAtToday.Any(a => a.Equals(email))) //今天的邮件要比对
                                {
                                    email.Dispose();
                                    //continue;
                                    break;
                                }
                                else
                                {
                                    _emailAtToday.Add(email);
                                    if (email.Date < _initTime.AddMinutes(-1))
                                    {
                                        email.Dispose();
                                        break;
                                    }
                                }
                            }
                            else //新邮件(没有这句的话,新邮件会读两次--benjamin20200414
                            {
                                if (!_emailAtToday.Any(a => a.Equals(email)))
                                {
                                    _emailAtToday.Add(email);
                                }
                            }
                        }
                        else
                        {
                            isAnyReceiveFail = true;
                        }
                        //if (PFDataHelper.IsDebug)
                        //{
                        //    PFDataHelper.WriteLog("7");
                        //}

                        var b = false;
                        try
                        {
                            b = SubjectMatch(email);
                        }
                        catch (Exception e)
                        {
                            newestMailTime = null;
                            PFDataHelper.WriteError(new Exception(string.Format("SubjectMatch报错{0}", e)));
                        }
                        if (b)
                        {
                            try
                            {
                                _emailManager.Disconnect_Click();

                                DoAction(email);
                            }
                            catch (Exception e)
                            {
                                PFDataHelper.WriteError(e);
                            }
                            email.Dispose();
                            ////_emailManager.DeleteEmail(email);
                            //_lastListenTime = email.Date ?? DateTime.Now;
                            if (_onlyListenOnce)
                            {
                                _running = false;
                                return;
                            }
                            break;
                        }

                        email.Dispose();
                    }
                    //if (PFDataHelper.IsDebug)
                    //{
                    //    PFDataHelper.WriteLog("11");
                    //}
                    //不管有没有匹配到邮件,下次都应该找时间更新的邮件了
                    if (newestMailTime != null && newestMailTime > _lastListenTime && (!isAnyReceiveFail))
                    {
                        _lastListenTime = newestMailTime.Value;
                    }

                    //if (PFDataHelper.IsDebug)
                    //{
                    //    PFDataHelper.WriteLog("12");
                    //}
                    _emailManager.Disconnect_Click();
                    //if (PFDataHelper.IsDebug)
                    //{
                    //    PFDataHelper.WriteLog("13");
                    //}
                    Thread.Sleep(CheckMessageInterval);
                    //if (PFDataHelper.IsDebug)
                    //{
                    //    PFDataHelper.WriteLog("14");
                    //}
                    GC.Collect();//一定要有句,否则SendMobileMessage里面的所有List会使内存越来越高

                    //if (PFDataHelper.IsDebug)
                    //{
                    //    PFDataHelper.WriteLog("15");
                    //}

                    RemoveEmailAtYesterday();
                }
                catch (Exception e)
                {
                    PFDataHelper.WriteError(e);
                    Thread.Sleep(CheckMessageInterval);
                }
            }
        }