0

我正在使用EWS API从 Exchange 2007 服务器连接和检索邮件,它工作正常。现在我想收到拉通知。我在这里找到了一个示例,这是我尝试过的:

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();

但是,当我在那里放置断点时,我从来没有到达上面的开关。我已经发送了一些邮件并删除了一些,但没有任何反应。

任何想法为什么这不起作用?或者还有其他方法可以获取拉取通知吗?

4

1 回答 1

3

我找到了解决方案。我上面所做的是创建一个新订阅并立即尝试获得结果。相反,我必须创建一次订阅,然后不时提取结果。

例子:

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();
于 2013-04-26T09:54:50.297 回答