我使用EWS API连接并检索来自Exchange 2007服务器的邮件,该工作正常。现在我想获得拉动通知。我找到了一个例子here,这是我尝试过的:
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();
但是,当我在那里放置断点时,我从未接触到上述开关。我发了几封邮件并删了一些,没有任何反应。
为什么这不起作用的任何想法?或者还有其他方法可以获得拉动通知吗?
答案 0 :(得分:0)
我找到了解决方案。我上面做的是创建一个新的订阅,并立即尝试获得结果。相反,我必须创建一次订阅,然后不时地提取结果。
示例:
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();