简体   繁体   中英

Detect email move using Exchange Web Service Managed API?

I'm using EWS Managed API and C#.

I want to know if it's possible to detect when an email is moved to another folder.

This is what I have so far:

static void SetPullNotifications(ExchangeService service)
{

    PullSubscription subscription = service.SubscribeToPullNotificationsOnAllFolders(
      5, null,
      EventType.Moved, EventType.Deleted, EventType.Copied, EventType.Modified);

    GetEventsResults events = subscription.GetEvents();

    foreach (ItemEvent itemEvent in events)
    {
        switch (itemEvent.EventType)
        {
            case EventType.Moved:
                MessageBox.Show("Item Moved :" + itemEvent.ItemId.UniqueId);
                break;
            case EventType.Deleted:
                MessageBox.Show("Item deleted: " + itemEvent.ItemId.UniqueId);
                break;
            case EventType.Copied:
                MessageBox.Show("Item Copied :" + itemEvent.ItemId.UniqueId);
                break;
            case EventType.Modified:
                MessageBox.Show("Item Modified :" + itemEvent.ItemId.UniqueId);
                break;
        }
    }
}

This works fine if I put a breakpoint on the method GetEvents() , then move an email. But without the breakpoint it does not work. The events contains no results.

Any ideas ?

your on the right way, but your missing something. Your code will only get the events which occour between creating the subscription and getting the Events, that's why it only works with a breakpoint.

To make your code work you should do 2 things. At first: Create the subscrption when you start your application and keep a refernce on it. At Second Store the Watermark you get from the Subscription and reload it on application startup. Maybe like this:

static PullSubscription s_Subscription;

static void Main()
{
    ExchangeService service = CreateService();
    CreateSubsciption(service);
    //DoSomething;
    GetEvents();
    //DoSomething;
    StoreWatermark(s_Subscription.Watermark);
}

static void CreateSubscription(ExchangeService Service)
{
    string watermarkt = LoadWatermark(); 
    s_Subscription = service.SubscribeToPullNotificationsOnAllFolders(
        5, watermark,
        EventType.Moved, EventType.Deleted, EventType.Copied, EventType.Modified);

}

static void GetEvents()
{

    GetEventsResults events = subscription.GetEvents();

    foreach (ItemEvent itemEvent in events)
    {
        switch (itemEvent.EventType)
        {
            case EventType.Moved:
                MessageBox.Show("Item Moved :" + itemEvent.ItemId.UniqueId);
                break;
            case EventType.Deleted:
                MessageBox.Show("Item deleted: " + itemEvent.ItemId.UniqueId);
                break;
            case EventType.Copied:
                MessageBox.Show("Item Copied :" + itemEvent.ItemId.UniqueId);
                break;
            case EventType.Modified:
                MessageBox.Show("Item Modified :" + itemEvent.ItemId.UniqueId);
                break;

        }
    }
}

You can use Streaming Notifications with EWS to listen for changes to items on the Exchange Server. Here is an example on how to set up Streaming Notifications:

http://blogs.msdn.com/b/exchangedev/archive/2010/12/22/working-with-streaming-notifications-by-using-the-ews-managed-api.aspx

In your case you should handle the EventType.Moved event. When you are handling the events you are given an object of type ItemEvent (as shown in the above example) which has two properties OldParentFolderId and ParentFolderId which identify the folder the item was moved from and to.

The reason, why your code doesnt work, is just simple. there is no time that Events could happen. You create a subcription which will recognize only events from the Moment you create it, 'cause the watermark is null. Only one line later, so let's say a millisecond later you ask the subscription "hey were there any Event in the past millisecond?" and the answer is "no". Create your subscription at program startup and call getevents on a timer, maybe 5 minutes later. And if you had any event in the past five minutes, your messagebox will appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM