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

标签 c# exchangewebservices email-client

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

相关文章:

c# - `BeginSubscribeToStreamingNotifications` 和 `SubscribeToStreamingNotifications` 有什么区别

exchangewebservices - 如何使用 FindItems() 获取在某个日期范围内开始的一次或多次重复出现的所有重复系列的经常性母版?

java - 从应用程序发送的电子邮件

c# - 格式化字符串的输出

c# - 测试 ViewModel PropertyChanged 事件

c# - 使用 EWS 托管 API 将 .msg 文件上传到 Exchange Server

android - 通过 Exchange 服务器从 Android 应用程序自动发送电子邮件

html - 如何在响应式电子邮件模板中堆叠列?

c# - Entity Framework IDENTITY_INSERT ON 不起作用

c# - 使用 OracleDataReader (ODAC) 返回 Oracle 存储过程