【问题标题】:EWS managed API streaming notification ...NewMail watcher using C# maildeleted event is not notifiedEWS 托管 API 流通知 ...使用 C# maildeleted 事件的 NewMail 观察程序未收到通知
【发布时间】:2015-01-27 15:00:50
【问题描述】:

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

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+"--------");
        }
    }
}

【问题讨论】:

    标签: c# ews-managed-api


    【解决方案1】:

    这是一个例子:

           _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,其中包含有关您上次同步过程的一些信息。所以下次线程将同步自上次同步以来的所有项目。 希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      相关资源
      最近更新 更多