gpt4 book ai didi

c# - EWS 托管 API 拉取通知以查看新电子邮件不起作用

转载 作者:行者123 更新时间:2023-12-02 00:33:15 24 4
gpt4 key购买 nike

我正在创建一个应用程序,它将在后台充当窗口服务。该应用程序将解析收件箱中的新电子邮件并保存附件。我尝试过流式通知,但由于连接在 30 分钟后断开,我想使用拉取通知。下面是我调试的代码,但我在控制台上看不到任何输出。一旦我运行应用程序,它就会关闭控制台窗口,因此不知道它是否正常工作。我希望在新电子邮件进入收件箱后立即查看它,因此需要一些指导如何实现这一目标。

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;
}



}
}

最佳答案

当您想要更新时,拉取通知要求您发出拉取请求(通过 GetEvents)。https://msdn.microsoft.com/en-us/library/office/dn458791%28v=exchg.150%29.aspx 中描述了通知方法之间的差异。您的代码没有循环,仅发出一个 GetItem 请求,这就是为什么它的行为方式与您描述的正常情况相同。

I want watch the new email as soon as it enters in the inbox so need some guidance how to achieve that.

如果您希望服务器在电子邮件到达时通知您,那么您需要查看推送或流通知 拉取通知要求您轮询服务器以获取更新。

I tried streaming notification but as the connection disconnects after 30 mints

这很正常,您只需要代码来重新连接和管理订阅,请参阅 http://blogs.msdn.com/b/emeamsgdev/archive/2013/04/16/ews-streaming-notification-sample.aspx一个好的样本。

干杯格伦

关于c# - EWS 托管 API 拉取通知以查看新电子邮件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com