0

我開發了一個小型控制檯應用程序來測試EWS StreamingSubscriptions/Notifications。過去我們使用推送通知,但理論上,在使用StreamingNotifications時,我應該能夠避免創建監聽器http端點及其所有問題(防火牆等)。StreamingNotifications不適用於Office 365/Exchange Online

所以,從我的本地機器;我這樣做:

static void Main(string[] args) 
    { 
     if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["PrimaryLabUserId"])) 
     { 
      throw new ArgumentNullException("Please provide a value for PrimaryLabUserId in app.config"); 
     } 
     _primaryLabUserId = ConfigurationManager.AppSettings["PrimaryLabUserId"]; 

     string ServiceAccountName = ConfigurationManager.AppSettings["ExchangeServiceAccountName"]; 
     string ServiceAccountPassword = ConfigurationManager.AppSettings["ExchangeServiceAccountPassword"]; 


     _service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
     _service.Credentials = new WebCredentials(ServiceAccountName, ServiceAccountPassword); 
     _service.AutodiscoverUrl(_primaryLabUserId, (x) => true); 
     _ewsUrl = _service.Url.AbsoluteUri; 

     var _connection = new StreamingSubscriptionConnection(_service, 30); 

     var sub = SubscribeForStreamingNotifications(); 

     _connection.AddSubscription(sub); 

     _connection.OnDisconnect += 
      new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect); 

     // set up subscriptions here. 
     _connection.OnNotificationEvent += 
        new StreamingSubscriptionConnection.NotificationEventDelegate(OnNewMail); 

     _connection.Open(); 
     Console.WriteLine("Listening streaming..."); 
     Console.ReadLine(); 


    } 

public static StreamingSubscription SubscribeForStreamingNotifications() 
    { 

        var folderIds = new List<FolderId>() 
     { 
      WellKnownFolderName.Inbox, 
      WellKnownFolderName.Calendar 
     }; 

        var eventTypes = new List<EventType>(); 
     eventTypes.Add(EventType.NewMail); 
     eventTypes.Add(EventType.Deleted); 
     eventTypes.Add(EventType.Moved); 
     eventTypes.Add(EventType.Created); 
     eventTypes.Add(EventType.Modified); 

     return _service.SubscribeToStreamingNotifications(folderIds, eventTypes.ToArray()); 
    } 

private static void OnNewMail(object sender, NotificationEventArgs args) 
    { 
     var test = args; 
     Console.WriteLine("Incoming"); 
    } 

認購初始化OK,但是當我發送新郵件到LabUser沒有任何反應。通知事件不會觸發。我嘗試pushnotifications一樣,它正在工作(在另一臺服務器上有一個公共http端點用於交換回叫)。 我想知道這可能與我的本地機器有什麼關係。

回答

0

我多麼愚蠢。我忘了模仿。由於我使用服務帳戶呼叫EWS,因此它當然會在該帳戶的郵箱中偵聽,除非您指定:

_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, _primaryLabUserId); 
相關問題