I wanted to keep track of email activity on a shared mailbox. My primary interest is when email is moved,deleted, and modified(category changed) Below is my subscription code:
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();
Here's my event handler:
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");
At first everything seems to run fine. However I noticed some events didn't get triggered. For example, if the user changed email category and then moved it immediately, not all events get handled. I watched the output on my console and didn't see "moved" event. What would be the cause of this?