首页 新闻 会员 周边

EWS操作邮件报错

0
悬赏园豆:100 [待解决问题]
复制代码
 1  public ExchangeService GetServices()
 2         {
 3                   System.Net.ServicePointManager.ServerCertificateValidationCallback
 4                 = delegate(
 5                     Object obj,
 6                     X509Certificate certificate,
 7                     X509Chain chain,
 8                     SslPolicyErrors errors
 9                     )
10                 {
11                     return true;
12                     // Validate the certificate and return true or false as appropriate.        // Note that it not a good practice to always return true because not        // all certificates should be trusted.    
13                 };
14             ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
15 
16             18             
string[] cfg = Config.UserName.Split('\\'); 19 service.Credentials = new WebCredentials(cfg[1], Config.Password, cfg[0]);

26 service.Url = new Uri(Config.Server); 27 28 return service; 29 }
复制代码

这样的代码会报错吗?

Raceme的主页 Raceme | 初学一级 | 园豆:108
提问于:2018-12-13 21:28
< >
分享
所有回答(1)
0

你得把你的异常也贴出来,不然没人知道你哪儿错了

chester·chen | 园豆:507 (小虾三级) | 2018-12-14 06:59

就是放在正式环境上然后就显示报错到这里  从日志上面看的

支持(0) 反对(0) Raceme | 园豆:108 (初学一级) | 2018-12-14 09:03

12/13/2018 12:21:01 PM:
EmailService:获取邮件失败:   at TonkNet.BDA.Core.Service.Common.ExchangeEmailReciver.GetMailMessage()
   at SendEmail.ReciveMessagesService.LoadNeadSendMessages()
---------------------------------------------

这是日志信息  GetMailMessage  写了try catch catch只有上面的代码没有写到try里面  估摸着就是这报的错

支持(0) 反对(0) Raceme | 园豆:108 (初学一级) | 2018-12-14 09:07

@Raceme: 多贴一点信息,信息太少,很少有人能看懂

支持(0) 反对(0) chester·chen | 园豆:507 (小虾三级) | 2018-12-14 09:30

@老六代码: 

 

List<EmailMessage> list = new List<EmailMessage>();
   Hashtable saos = new Hashtable();
   try
   {
       if (this.pop3Cofig == null)
       {
           pop3Cofig = LoadPop3Config();
       }
                EmailReciver reviver = null;
                //string IsExchange = System.Configuration.ConfigurationSettings.AppSettings["aaaaa"].ToString();
                string IsExchange = System.Configuration.ConfigurationSettings.AppSettings["IsExchange"].ToString();
                if (IsExchange=="true")
                {
                    reviver = new ExchangeEmailReciver(Conf.appName, pop3Cofig);
                }
                else
                {
                    reviver = new Pop3EmailReciver(Conf.appName, pop3Cofig);
                }
                reviver.ReciveEndCallback += new EmailReciver.ReciveEnd(ReciveEndCallback);
                list = reviver.GetMailMessage();
   }
   catch (Exception error)
   {
    Log("获取邮件失败:" + error.StackTrace);
                Log(string.Format("老时间:{0},新时间{1}", SendTime.ToString(),DateTime.Now.ToString()));
                TimeSpan ts = DateTime.Now - SendTime;
                if (ts.TotalMinutes > 30)
                {
                    SendTime=DateTime.Now;
                    //发送邮件给固定人提醒服务错误
                    string MailTo = Conf.monitorMail;
                    string MailFrom = Conf.mailFrom;
                    string Title = "邮箱服务出现异常,请查看详细信息并联系管理员";
                    string Opt = "HRMLEmailReciveService";
                    SelectData("Proc_Sys_CreateEmailForOAApp", new string[] { "@Sender", "@SendFromEmail", "@SendTo", "@SendToEmail", "@EmailTitle", "@EmailContext", "@Opt" }, new object[] { MailFrom, MailFrom, MailTo, MailTo, Title, error.StackTrace, Opt });
                }
   }
            return list;

 

日志报错信息

at TonkNet.BDA.Core.Service.Common.ExchangeEmailReciver.GetMailMessage()
   at SendEmail.ReciveMessagesService.LoadNeadSendMessages()

ExchangeEmailReciver.GetMailMessage方法如下:

public override List<EmailMessage> GetMailMessage()
        {
            var list = new List<EmailMessage>();
            var service = GetServices();
            try
            {

                //创建过滤器, 条件为邮件未读. 
                SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
                FindItemsResults<Item> findResults = service.FindItems(
                    WellKnownFolderName.Inbox,
                    new ItemView(100));
                int messageCount = findResults.TotalCount; // client.GetMessageCount();

                IList<ItemId> deleteIndexes = new List<ItemId>();
                DateTime lastUpdateTime = DateTime.Now;
                int index = Config.LastReciveIndex < 1 ? 1 : Config.LastReciveIndex;

                if (findResults != null)
                {
                    foreach (Item item in findResults.Items)
                    {
                        Config.LastReciveIndex = index + 1;
                        Microsoft.Exchange.WebServices.Data.EmailMessage email =
                            Microsoft.Exchange.WebServices.Data.EmailMessage.Bind(service, item.Id);
                        lastUpdateTime = email.LastModifiedTime;
                        if (lastUpdateTime.CompareTo(Config.LastReciveTime) > 0)
                        {
                            if (Config.DeleteRemoteEmail)
                            {
                                deleteIndexes.Add(item.Id);
                            }
                            var subject = email.Subject;
                            var body = email.Body;
                            var receivedDate = email.DateTimeReceived;
                            var em = new TonkNet.BDA.Core.Kernel.Message.EmailMessage(email.From.Address, subject,
                                                                                      body.Text, receivedDate);
                            em.MessageID = item.ConversationId.UniqueId;
                            list.Add(em);
                        }
                        ////标记为已读 
                        //email.IsRead = true;
                        ////将对邮件的改动提交到服务器 
                        //email.Update(ConflictResolutionMode.AlwaysOverwrite);
                        //email.Delete(DeleteMode.MoveToDeletedItems);
                    }
                }
                if (Config.DeleteRemoteEmail)
                {
                    if (deleteIndexes.Count != 0)
                    {
                        service.DeleteItems(deleteIndexes, DeleteMode.MoveToDeletedItems, null, null);
                    }
                    Config.LastReciveIndex = 1;
                }
                if (ReciveEndCallback != null)
                {
                    ReciveEndCallback(this.AppName, lastUpdateTime, Config.LastReciveIndex, list);
                }

            }
            catch (Exception e)
            {
               
            }
            return list;
        }

 

写了try catch 如何是try里面的报错,应该会返回list吧?

所以我估摸着是GetServices()报错了

 

GetServices()方法如下:

public ExchangeService GetServices()
        {
            System.Net.ServicePointManager.ServerCertificateValidationCallback
                = delegate(
                    Object obj,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors errors
                    )
                {
                    return true;
                    // Validate the certificate and return true or false as appropriate.  // Note that it not a good practice to always return true because not  // all certificates should be trusted. 
                };
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

            // Setting credentials is unnecessary when you connect from a computer that is
            // logged on to the domain.
            string[] cfg = Config.UserName.Split('\\');
            service.Credentials = new WebCredentials(cfg[1], Config.Password, cfg[0]);
            service.Url = new Uri(Config.Server);

            return service;
        }

支持(0) 反对(0) Raceme | 园豆:108 (初学一级) | 2018-12-14 09:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册