我想跟踪共享邮箱上的电子邮件活动。我的主要兴趣是当邮件被移动,删除和修改(类别变化)下面是我的申购代码:EWS API连接意外关闭
StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(
GetFolders(),
EventType.NewMail,
EventType.Modified,//if the user modified the category
EventType.Deleted,EventType.Moved);
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service,30);
connection.AddSubscription(streamingsubscription);
// Delegate event handlers.
connection.OnNotificationEvent +=
new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.OnSubscriptionError +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
connection.OnDisconnect +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
connection.Open();
这里是我的事件处理程序:
StreamingSubscription subscription = args.Subscription;
var events = args.Events.Select(x => x.EventType.ToString()).ToArray();
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "############Events Detected " + String.Join(",",events));
foreach (NotificationEvent notification in args.Events)
{
if (notification is ItemEvent)
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "Handling Item Event");
ItemEvent itemEvent;
switch (notification.EventType)
{
case EventType.NewMail:
itemEvent = (ItemEvent)notification;
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Mail Received:" + itemEvent.ItemId);
Save(itemEvent.ItemId, itemEvent.ParentFolderId.UniqueId);
break;
case EventType.Moved:
itemEvent = (ItemEvent)notification;
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item Moved:" + itemEvent.OldItemId + " === " + itemEvent.ItemId);
Update(itemEvent.OldItemId, itemEvent.ItemId, itemEvent.ParentFolderId);
break;
case EventType.Deleted:
itemEvent = (ItemEvent)notification;
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item deleted:" + itemEvent.ItemId);
Delete(itemEvent.ItemId);
break;
case EventType.Modified:
itemEvent = (ItemEvent)notification;
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item Modified:" + itemEvent.ItemId);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item Changed Detected-----------");
Modify(itemEvent.ItemId);
break;
}
}
else
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "------------Ignoring Folder Event");
}
}
Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "#######Done");
起初一切看上去一切正常。但是我注意到一些事件没有被触发。例如,如果用户更改了电子邮件类别,然后立即移动它,则并非所有事件都得到处理。我看到我的控制台上的输出,并没有看到“移动”事件。这会是什么原因?
我建议你使用EWSeditor https://ewseditor.codeplex.com/(使用Streaming通知查看器)进行一些测试,并监听所有事件,这些事件应该让您更清楚地了解服务器通知的内容。 –
我会尝试。我发现一些很有趣的东西。我注意到我的连接因“System.Net.Sockets.SocketException”而关闭。我尝试过各种错误处理,但无法捕捉到异常。但是,通过将Outlook中的多个电子邮件从一个文件夹移动到另一个文件夹,我能够导致连接出错。我不知道为什么这样的行为会导致连接关闭。深入挖掘,我收到错误消息“操作超时” – user1784014