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

EWS managed API Pull notification to watch the new email is not working
2020-11-24
  •  译文(汉语)
  •  原文(英语)

我正在创建一个将在后台用作窗口服务的应用程序.该应用程序将解析收件箱中的新电子邮件并保存附件.我尝试了流式通知,但由于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;
        }   



    }
}
解决过程1

提取通知要求您在要更新时(通过GetEvents)发出提取请求,参见https://msdn.microsoft.com/zh-cn/library/office/dn458791%28v=exchg中所述的通知方法之间的差异. 150%29.aspx.您的代码没有循环,仅发出一个GetItem请求,这就是为什么它表现出您描述正常的方式.

我想立即在收件箱中输入新电子邮件,因此需要一些指导来实现.

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

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

正常情况下,您只需要代码即可重新连接和管理订阅,请参见http://blogs.msdn.com/b/emeamsgdev/archive/2013/04/16/ews-streaming-notification-sample.aspx以获取良好的示例.

干杯格伦

速聊1:
谢谢您的帮助.
速聊2:
我在您提供的链接中浏览了示例代码.我在流式通知应用程序中进行了一些更改.基本上,我重新连接了连接,但是关于订阅,我仍然不确定如何进行管理.因为我想解析在连接过程中收到的电子邮件,所以请断开连接.
速聊3:
为了管理订阅,我将只使用看门狗线程,使用流式通知时,您应该不断获取心跳更新,因此,如果您开始缺少更新,则可能表明流已中断.通常,您只需要重新连接订阅,或者在订阅无效时创建一个新订阅.对于断开连接期间丢失的任何通知,使用"拉"通知或SyncfolderItems都很方便.
速聊4:
我正在运行尖叫的通知应用程序.我在回路中重新连接了连接.您可以在我确实需要进行更改的地方进行测试吗?通常,我通过在重新连接线上添加断点来测试应用程序,并等待一段时间,然后在这段时间内连接并发送少量电子邮件,然后再次继续调试应用程序.我观察到,一旦建立连接,它将解析在连接断开和重新连接时间范围之间发送的电子邮件.我可以根据需要添加代码.
速聊5:
正是由于我需要.

I am creating a application which will work as a window service in the back ground. This application will parse the new emails in the inbox and save the attachments. I tried streaming notification but as the connection disconnects after 30 mints I thought to use pull notifications. Below is the code which I debug but I don't see any output on console. As soon as I run the application it closes the console window so don't know if it is working. I want watch the new email as soon as it enters in the inbox so need some guidance how to achieve that.

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



    }
}
Solutions1

Pull notifications require that you issue a pull request (via GetEvents) when you want updates the difference between the notification methods is described in https://msdn.microsoft.com/en-us/library/office/dn458791%28v=exchg.150%29.aspx .Your code has no loop and only issue one GetItem request which is why it behaves the way you describe which is normal.

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

If you want the Server to notify you when an email has arrived then you need to either look at Push or Streaming notifications Pull notifications requires that you poll the server for updates.

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

That's normal you just need code to reconnect and manage the subscription see http://blogs.msdn.com/b/emeamsgdev/archive/2013/04/16/ews-streaming-notification-sample.aspx for a good sample.

Cheers Glen

Talk1:
Thank you for your help.
Talk2:
I went through the sample code in the link provided by you. I made some changes in my streaming notification application . basically I reconnect the connection its working but about subscription I am still not sure how to mange that. As I want parse email which has been received during the connection dissconect and connect.
Talk3:
To manage the subscription i'd just use a watchdog thread you should be getting constant heartbeat updates when using Streaming notifications so if you start missing the updates it may indicate the stream is broken. Generally you just then need to reconnect the Subscription or create a new one if its invalid. For any missing notifications during the disconnect this is where using Pull notifications or SyncfolderItems comes in handy.
Talk4:
I have screaming notification application running . I reconnect the connection in the loop. Can you test it where exactly I need to make changes. Bsically I tested application by adding break point at the reconnect line and waited for some time before it connects and send few e,mails during that time then again continue debugging the application . I observerd that that once the connection is setup gain it parsed the emails which were sent between the connection disconnect and reconnect time frame. I can add code if you want.
Talk5:
Thanks exactly what i needed.
转载于:https://stackoverflow.com/questions/28158092/ews-managed-api-pull-notification-to-watch-the-new-email-is-not-working

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入