EWS管理的API流通知…未通知使用C#maildeleted事件的NewMail watcher

EWS管理的API流通知…未通知使用C#maildeleted事件的NewMail watcher,c#,ews-managed-api,C#,Ews Managed Api,将订阅流媒体通知的.Net应用程序。此应用程序运行良好,此订阅在30分钟后断开连接,因此我添加了代码以重新连接。我通过在重新连接行添加断点来测试应用程序,并等待了一段时间,然后再次建立连接。在此期间,我创建了一些新的电子邮件,并观察console是否显示这些事件。当连接断开时,它并没有出现,然后我再次运行代码,我能够看到在连接断开和连接期间创建的事件。我需要知道这是否是使此过程连续运行并跟踪应用程序断开和重新连接期间发生的事件的正确方法。还需要知道为什么下面的代码没有通知删除邮件事件。诚恳地寻求

将订阅流媒体通知的.Net应用程序。此应用程序运行良好,此订阅在30分钟后断开连接,因此我添加了代码以重新连接。我通过在重新连接行添加断点来测试应用程序,并等待了一段时间,然后再次建立连接。在此期间,我创建了一些新的电子邮件,并观察console是否显示这些事件。当连接断开时,它并没有出现,然后我再次运行代码,我能够看到在连接断开和连接期间创建的事件。我需要知道这是否是使此过程连续运行并跟踪应用程序断开和重新连接期间发生的事件的正确方法。还需要知道为什么下面的代码没有通知删除邮件事件。诚恳地寻求帮助

namespace NewMailNotification
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            //***********New**********************
            ExchangeService  mailbox = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
            string mailboxEmail = ConfigurationSettings.AppSettings["user-id"]; 
            WebCredentials wbcred = new WebCredentials(ConfigurationSettings.AppSettings["user"], ConfigurationSettings.AppSettings["PWD"]); 
            mailbox.Credentials = wbcred;
        //    mailbox.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mailboxEmail);

            mailbox.AutodiscoverUrl(mailboxEmail, RedirectionUrlValidationCallback);
            mailbox.HttpHeaders.Add("X-AnchorMailBox", mailboxEmail);
            FolderId mb1Inbox = new FolderId(WellKnownFolderName.Inbox, mailboxEmail);
            SetStreamingNotification(mailbox, mb1Inbox);
            bool run = true;
            bool reconnect = false;
            while (run)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

        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;
        }

        static void SetStreamingNotification(ExchangeService service,FolderId fldId)
        {
            StreamingSubscription streamingssubscription=service.SubscribeToStreamingNotifications(new FolderId[]{fldId},
                EventType.NewMail,
                EventType.Created,
                EventType.Deleted);

            StreamingSubscriptionConnection connection=new StreamingSubscriptionConnection(service,30);
            connection.AddSubscription(streamingssubscription);

            //Delagate event handlers
            connection.OnNotificationEvent+=new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
            connection.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(Connection_OnDisconnect);
            connection.OnSubscriptionError+=new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
            connection.Open();

        }

        static private void Connection_OnDisconnect(object sender, SubscriptionErrorEventArgs args)
        {
            StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
            if (!connection.IsOpen)             
            {
                //  Console.WriteLine("no connection");
                connection.Open();
            }
        }

        static void OnEvent(object sender,NotificationEventArgs args)
        {
            StreamingSubscription subscription=args.Subscription;
            if(subscription.Service.HttpHeaders.ContainsKey("X-AnchorMailBox"))
            {
                Console.WriteLine("event for nailbox"+subscription.Service.HttpHeaders["X-AnchorMailBox"]);
            }
            //loop through all the item-related events.
            foreach(NotificationEvent notification in args.Events)
            {
                switch(notification.EventType)
                {
                    case EventType.NewMail:
                        Console.WriteLine("\n----------------Mail Received-----");
                        break;
                    case EventType.Created:
                        Console.WriteLine("\n-------------Item or Folder deleted-------");
                        break;
                    case EventType.Deleted:
                        Console.WriteLine("\n------------Item or folder deleted---------");
                        break;

                }

                //Display notification identifier
                if(notification is ItemEvent)
                {
                    //The NotificationEvent for an email message is an ItemEvent
                    ItemEvent itemEvent=(ItemEvent)notification;
                    Console.WriteLine("\nItemId:"+ itemEvent.ItemId.UniqueId);
                    Item NewItem=Item.Bind(subscription.Service,itemEvent.ItemId);
                    if(NewItem is EmailMessage)
                    {
                        Console.WriteLine(NewItem.Subject);
                    }

                }
                else
                {
                    //the Notification for a Folder is an FolderEvent
                    FolderEvent folderEvent=(FolderEvent)notification;
                    Console.WriteLine("\nFolderId:"+folderEvent.FolderId.UniqueId);
                }
            }
        }
        static void OnError(object sender,SubscriptionErrorEventArgs args)
        {
            //Handle error conditions.
            Exception e=args.Exception;
            Console.WriteLine("\n-----Error-----"+e.Message+"--------");
        }
    }
}
以下是一个例子:

       _BackroundSyncThread = new Thread(streamNotification.SynchronizeChangesPeriodically);       _BackroundSyncThread.Start();

 private void SynchronizeChangesPeriodically()
    {
      while (true)
      {
        try
        {
          // Get all changes from the server and process them according to the business
          // rules.
          SynchronizeChanges(new FolderId(WellKnownFolderName.Calendar));
        }
        catch (Exception ex)
        {
          Console.WriteLine("Failed to synchronize items. Error: {0}", ex);
        }
        // Since the SyncFolderItems operation is a 
        // rather expensive operation, only do this every 10 minutes
        Thread.Sleep(TimeSpan.FromMinutes(10));
      }
    }
    public void SynchronizeChanges(FolderId folderId)
    {
      bool moreChangesAvailable;
      do
      {
        Debug.WriteLine("Synchronizing changes...");
        // Get all changes since the last call. The synchronization cookie is stored in the _SynchronizationState field.
        // Only the the ids are requested. Additional properties should be fetched via GetItem calls.
        var changes = _ExchangeService.SyncFolderItems(folderId, PropertySet.FirstClassProperties, null, 512,
                                                       SyncFolderItemsScope.NormalItems, _SynchronizationState);
        // Update the synchronization cookie
        _SynchronizationState = changes.SyncState;

        // Process all changes
        foreach (var itemChange in changes)
        {
          // This example just prints the ChangeType and ItemId to the console
          // LOB application would apply business rules to each item.
          Console.WriteLine("ChangeType = {0}", itemChange.ChangeType);
          Console.WriteLine("Subject = {0}", itemChange.Item.Subject);
        }
        // If more changes are available, issue additional SyncFolderItems requests.
        moreChangesAvailable = changes.MoreChangesAvailable;
      } while (moreChangesAvailable);
    }
_SynchronizationState字段类似于一个Cookie,其中包含有关上次同步过程的一些信息。因此,下一次线程将同步自上次同步以来的所有项目。 希望能有帮助