0
votes

I am using the EWS API to connect and retrieve mail from a exchange 2007 server, which works fine. Now I would like to get pull notifications. I have found a example here, which is what I have tried:

public IEnumerable<ItemEvent> GetPullNotifications(FolderId folderId)
{
    PullSubscription subscription = Service.SubscribeToPullNotifications(new FolderId[] { folderId }, 5, null, EventType.NewMail, EventType.Created, EventType.Deleted);
    return subscription.GetEvents().ItemEvents;
}

public void CheckPullNotifications(object source, ElapsedEventArgs e)
{
    Console.WriteLine("Check...");
    IEnumerable<ItemEvent> itemEvents = ewsClient.GetPullNotifications(WellKnownFolderName.Inbox);
    foreach (ItemEvent itemEvent in itemEvents)
    {
        switch (itemEvent.EventType)
        {
            case EventType.NewMail:
                MessageBox.Show("New mail: " + itemEvent.ItemId.UniqueId);
                break;
            case EventType.Deleted:
                MessageBox.Show("Mail deleted: " + itemEvent.ItemId.UniqueId);
                break;
        }
    }

    return;
}

//...
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(CheckPullNotifications);
myTimer.Interval = 1000;
myTimer.Start();

However, I never get to the above switch when I put a breakpoint there. I've send a few mails and deleted some, nothing happens.

Any ideas why this does not work? Or are there any other ways to get pull notifications?

1

1 Answers

3
votes

I found a solution. What I did above was creating a new subscription and instantly trying to get the results. Instead, I have to create a subscription once and then pull the results every now and then.

Example:

public void SubscribePullNotifications(FolderId folderId)
{
    Subscription = Service.SubscribeToPullNotifications(new FolderId[] { folderId }, 1440, null, EventType.NewMail, EventType.Created, EventType.Deleted);
}

public void GetPullNotifications()
{
    IEnumerable<ItemEvent> itemEvents = Subscription.GetEvents().ItemEvents;
    foreach (ItemEvent itemEvent in itemEvents)
    {
        switch (itemEvent.EventType)
        {
            case EventType.NewMail:
                MessageBox.Show("New Mail");
                break;
        }
    }
}
// ...
SubscribePullNotifications(WellKnownFolderName.Inbox);
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(GetPullNotifications);
myTimer.Interval = 10000;
myTimer.Start();