2015-01-26 65 views
1

我正在创建一个应用程序,它将在后台作为窗口服务工作。此应用程序将解析收件箱中的新电子邮件并保存附件。我尝试了流媒体通知,但随着连接断开30个薄荷糖后,我想使用拉通知。下面是我调试的代码,但是在控制台上看不到任何输出。只要我运行该应用程序,它将关闭控制台窗口,因此不知道它是否正常工作。我想在收到新邮件后立即看新邮件,所以需要一些指导如何实现这一点。EWS托管的API拉取通知以观看新邮件不起作用

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Microsoft.Exchange.WebServices.Data; 
using System.Configuration; 
using System.Timers; 

namespace PullNotification 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
      WebCredentials wbcred = new WebCredentials(ConfigurationSettings.AppSettings["user"], ConfigurationSettings.AppSettings["PWD"]); 
      PullSubscription SubscriptionInbox; 

      service.Credentials = wbcred; 
      service.AutodiscoverUrl(ConfigurationSettings.AppSettings["user-id"], RedirectionUrlValidationCallback); 


      SubscriptionInbox = service.SubscribeToPullNotifications(new FolderId[] { WellKnownFolderName.Inbox }, 5/* subcription will end if the server is not polled within 5 mints*/, null/*to start a new subcription*/, EventType.NewMail, EventType.Modified); 
      //Timer myTimer = new Timer(); 
      //myTimer.Elapsed += new ElapsedEventHandler(GetPullNotifications); 
      //myTimer.Interval = 10000; 
      //myTimer.Start(); 


      GetEventsResults events = SubscriptionInbox.GetEvents(); 
      EmailMessage message; 

      foreach (ItemEvent itemEvent in events.ItemEvents) 
      { 
       switch (itemEvent.EventType) 
       { 
        case EventType.NewMail: 
         try 
         { 
          Item item = Item.Bind(service, itemEvent.ItemId); 
          if (item.Subject == "A123") 
          { 
           Console.WriteLine("check the code"); 
          } 
         } 
         catch (Exception e) 
         { 
          Console.WriteLine("error=" + e.Message); 
         } 
         break; 
        case EventType.Deleted: 
         Item item1 = Item.Bind(service, itemEvent.ItemId); 
         Console.WriteLine("Mail with subject" + item1.Subject + "--is deleted"); 
         break; 
       } 
      } 
      //Loop 




       } 





     internal static bool RedirectionUrlValidationCallback(string redirectionUrl) 
     { 
      //The default for the validation callback is to reject the URL 
      bool result=false; 

      Uri redirectionUri=new Uri(redirectionUrl); 
      if(redirectionUri.Scheme=="https") 
      { 
       result=true; 
      } 
      return result; 
     } 



    } 
} 

回答

4

拉的通知要求发出拉入请求(通过GetEvents)当您想更新的通知方法之间的差异https://msdn.microsoft.com/en-us/library/office/dn458791%28v=exchg.150%29.aspx。您的代码说明没有循环,只有一个问题的GetItem请求,这是为什么呢按照您描述的方式行事,这是正常的。

我想,只要它在收件箱中进入观看新的电子邮件,以便需要一些指导如何实现这一目标。

如果你想在服务器时通知您的电子邮件已经到达,那么你需要要不是看在推或要求您轮询更新服务器流通知拉通知。

我试图流通知,但30个作为连接断开后薄荷糖

这是正常的,你只需要代码重新连接和管理订阅看到http://blogs.msdn.com/b/emeamsgdev/archive/2013/04/16/ews-streaming-notification-sample.aspx一个很好的示例。

Cheers Glen

+0

谢谢你的帮忙。 – user2897967 2015-01-27 14:54:20

+0

我检查了您提供的链接中的示例代码。我在流式通知应用程序中做了一些更改。基本上我重新连接它的工作连接,但关于订阅我仍然不知道如何管理。因为我想解析连接期间收到并连接的电子邮件。 – user2897967 2015-01-27 18:00:11

+0

要管理订阅,我只是使用一个看门狗线程,您应该在使用流式通知时获得不断的心跳更新,所以如果您开始丢失更新,它可能表示该流已损坏。一般来说,您只需要重新连接订阅或创建一个新的订阅,如果它无效。对于断开连接期间的任何丢失通知,这是使用Pull通知或SyncfolderItems派上用场的地方。 – 2015-01-28 03:06:43

相关问题