C# EWS管理的用于监视新电子邮件的API拉取通知不起作用

C# EWS管理的用于监视新电子邮件的API拉取通知不起作用,c#,exchangewebservices,email-client,C#,Exchangewebservices,Email Client,我正在创建一个应用程序,它将作为后台的窗口服务。此应用程序将解析收件箱中的新电子邮件并保存附件。我尝试了流式通知,但由于连接在30分钟后断开,我想使用拉式通知。下面是我调试的代码,但在控制台上看不到任何输出。我一运行应用程序,它就会关闭控制台窗口,因此不知道它是否工作。我想在新邮件进入收件箱后立即观看它,因此需要一些如何实现这一目标的指导 using System; using System.Collections.Generic; using System.Linq; using System

我正在创建一个应用程序,它将作为后台的窗口服务。此应用程序将解析收件箱中的新电子邮件并保存附件。我尝试了流式通知,但由于连接在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)。中描述了通知方法之间的差异。您的代码没有循环,只发出一个GetItem请求,这就是为什么它的行为与您描述的正常方式相同

我想在新邮件进入收件箱后立即观看它,因此需要一些如何实现这一目标的指导

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



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

我尝试了流式通知,但由于连接在30分钟后断开

这很正常,您只需要重新连接和管理订阅所需的代码,请参阅以获取良好的示例

干杯
Glen

感谢您的帮助。我浏览了您提供的链接中的示例代码。我在流媒体通知应用程序中做了一些更改。基本上,我重新连接它的工作,但关于订阅,我仍然不知道如何管理它。因为我想解析在连接断开和连接期间收到的电子邮件。要管理订阅,我只需使用一个看门狗线程,在使用流式通知时,您应该获得恒定的心跳更新,因此如果您开始缺少更新,则可能表明流已断开。通常,您需要重新连接订阅,或者在订阅无效时创建一个新的订阅。对于断开连接期间丢失的任何通知,这是使用Pull通知或SyncfolderItems的方便之处。我已经运行了尖叫通知应用程序。我重新连接循环中的连接。你能在我需要修改的地方测试一下吗。实际上,我通过在重新连接线上添加断点来测试应用程序,并在连接之前等待了一段时间,在此期间发送了几封电子邮件,然后再次继续调试应用程序。我观察到,一旦建立连接,它就会解析在连接断开和重新连接时间段之间发送的电子邮件。如果你需要的话,我可以添加代码。非常感谢我所需要的。