我正在创建一个应用程序,它将在后台作为窗口服务工作。此应用程序将解析收件箱中的新电子邮件并保存附件。我尝试了流媒体通知,但随着连接断开30个薄荷糖后,我想使用拉通知。下面是我调试的代码,但是在控制台上看不到任何输出。只要我运行该应用程序,它将关闭控制台窗口,因此不知道它是否正常工作。我想在收到新邮件后立即看新邮件,所以需要一些指导如何实现这一点。EWS托管的API拉取通知以观看新邮件不起作用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;
using System.Configuration;
using System.Timers;
namespace PullNotification
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
WebCredentials wbcred = new WebCredentials(ConfigurationSettings.AppSettings["user"], ConfigurationSettings.AppSettings["PWD"]);
PullSubscription SubscriptionInbox;
service.Credentials = wbcred;
service.AutodiscoverUrl(ConfigurationSettings.AppSettings["user-id"], RedirectionUrlValidationCallback);
SubscriptionInbox = service.SubscribeToPullNotifications(new FolderId[] { WellKnownFolderName.Inbox }, 5/* subcription will end if the server is not polled within 5 mints*/, null/*to start a new subcription*/, EventType.NewMail, EventType.Modified);
//Timer myTimer = new Timer();
//myTimer.Elapsed += new ElapsedEventHandler(GetPullNotifications);
//myTimer.Interval = 10000;
//myTimer.Start();
GetEventsResults events = SubscriptionInbox.GetEvents();
EmailMessage message;
foreach (ItemEvent itemEvent in events.ItemEvents)
{
switch (itemEvent.EventType)
{
case EventType.NewMail:
try
{
Item item = Item.Bind(service, itemEvent.ItemId);
if (item.Subject == "A123")
{
Console.WriteLine("check the code");
}
}
catch (Exception e)
{
Console.WriteLine("error=" + e.Message);
}
break;
case EventType.Deleted:
Item item1 = Item.Bind(service, itemEvent.ItemId);
Console.WriteLine("Mail with subject" + item1.Subject + "--is deleted");
break;
}
}
//Loop
}
internal static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
//The default for the validation callback is to reject the URL
bool result=false;
Uri redirectionUri=new Uri(redirectionUrl);
if(redirectionUri.Scheme=="https")
{
result=true;
}
return result;
}
}
}
谢谢你的帮忙。 – user2897967 2015-01-27 14:54:20
我检查了您提供的链接中的示例代码。我在流式通知应用程序中做了一些更改。基本上我重新连接它的工作连接,但关于订阅我仍然不知道如何管理。因为我想解析连接期间收到并连接的电子邮件。 – user2897967 2015-01-27 18:00:11
要管理订阅,我只是使用一个看门狗线程,您应该在使用流式通知时获得不断的心跳更新,所以如果您开始丢失更新,它可能表示该流已损坏。一般来说,您只需要重新连接订阅或创建一个新的订阅,如果它无效。对于断开连接期间的任何丢失通知,这是使用Pull通知或SyncfolderItems派上用场的地方。 – 2015-01-28 03:06:43