EWS 托管 API 收看新电子邮件的拉取通知不起作用

EWS managed API Pull notification to watch the new email is not working

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

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 以获得好的示例。

干杯 格伦