【问题标题】:EWS subscribe to streaming notifications in the inboxEWS 订阅收件箱中的流式通知
【发布时间】:2017-05-13 05:55:25
【问题描述】:

我尝试编写一个控制台应用程序,它将使用 EWS 建立与邮箱的连接,然后在每次收到新电子邮件时打印一行。

完成这项工作后的最终结果是将其变成一项服务,并在每次电子邮件到达某个邮箱时创建一个任务,但现在我无法让控制台在收到后写一行。

控制台应用程序项目

Program.cs

class Program
{
    static void Main(string[] args)
    {
         ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
         WebCredentials wbcred = new WebCredentials("myusername", "mypassw0rd");
         service.Credentials = wbcred;
         service.AutodiscoverUrl("myemailaddress@mydomain.com", RedirectionUrlValidationCallback);

         EWSConnection.SetStreamingNotifications(service);
    }

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

类库项目

EWSConnection.cs

  public static void SetStreamingNotifications(ExchangeService service)
        {
            StreamingSubscription subscription;

            // Subscribe to streaming notifications in the Inbox. 
            subscription = service.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Inbox },
                EventType.NewMail);

            // Create a streaming connection to the service object, over which events are returned to the client.
            // Keep the streaming connection open for 30 minutes.
            StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
            connection.AddSubscription(subscription);
            connection.OnNotificationEvent += OnEvent;
            connection.OnDisconnect += OnDisconnect;
            connection.Open();

            bool status = connection.IsOpen;
            Console.WriteLine($"Connection Open:{status}");
        }

如果需要,我可以添加 OnEventOnDisconnect 方法。发生了什么是控制台打印

Connection Open:True 按任意键继续。 . .

然后,当我向该邮箱发送电子邮件时,没有任何反应,没有触发断点,也没有任何内容输出到控制台,这就是这两种方法的作用。

为什么我的OnEvent 方法没有触发?

编辑 - OnEvent 和 OnDisconnect 方法

 public static void OnEvent(object sender, NotificationEventArgs args)
        {
            StreamingSubscription subscription = args.Subscription;

            // Loop through all item-related events. 
            foreach (NotificationEvent notification in args.Events)
            {

                switch (notification.EventType)
                {
                    case EventType.NewMail:
                        Console.WriteLine("\n————-Mail created:————-");
                        break;
                    case EventType.Created:
                        Console.WriteLine("\n————-Item or folder created:————-");
                        break;
                    case EventType.Deleted:
                        Console.WriteLine("\n————-Item or folder deleted:————-");
                        break;
                }
                // Display the notification identifier. 
                if (notification is ItemEvent)
                {
                    // The NotificationEvent for an e-mail message is an ItemEvent. 
                    ItemEvent itemEvent = (ItemEvent)notification;
                    Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
                }
                else
                {
                    // The NotificationEvent for a folder is an FolderEvent. 
                    FolderEvent folderEvent = (FolderEvent)notification;
                    Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
                }
            }
        }

public static void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
    // Cast the sender as a StreamingSubscriptionConnection object.           
    StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
    // Ask the user if they want to reconnect or close the subscription. 
    ConsoleKeyInfo cki;
    Console.WriteLine("The connection to the subscription is disconnected.");
    Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
    while (true)
    {
        cki = Console.ReadKey(true);
        {
            if (cki.Key == ConsoleKey.Y)
            {
                connection.Open();
                Console.WriteLine("Connection open.");
                break;
            }
            else if (cki.Key == ConsoleKey.N)
            {
                // The ReadKey in the Main() consumes the E. 
                Console.WriteLine("\n\nPress E to exit");
                break;
            }
        }
    }
}

【问题讨论】:

  • 几个问题:您的 OnEvent 委托签名是否正确?您可以为代表提供您的代码吗? OnDisconnect 会触发吗?
  • 什么都不会触发,如果我在启动时在任一事件中设置调试,它会立即完成运行

标签: c# exchangewebservices


【解决方案1】:

令人讨厌的是,我错过了 Console.ReadKey() 方法。添加后它按预期工作...

    static void Main(string[] args)
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
        WebCredentials wbcred = new WebCredentials("myusername", "mypassw0rd","myDomain");
        service.Credentials = wbcred;
        service.AutodiscoverUrl("myemailaddress@mydomain.com", RedirectionUrlValidationCallback);

        EWSConnection.SetStreamingNotifications(service);

        Console.ReadKey(); //<-- this was missing
    }

【讨论】:

    【解决方案2】:

    将方法绑定到当前订阅上

    connection.OnNotificationEvent +=
                    new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
    connection.OnDisconnect +=
            new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect); 
    

    这是example

    【讨论】:

    • 这就是我最初的方式,但是 StreamingSubscriptionConnection.NotifcationEventDelegate 行是灰色的,上面写着“冗余显式委托创建”
    • @JsonStatham 你得到编译错误还是这个重构告诉你?
    • 没有复杂错误,只是智能提示它的冗余代码。不管有没有它,偶数都不会触发。
    • @JsonStatham 嗯..除了出现故障之外,我什么也找不到。
    • @JsonStatham 好的,那么我可以提供的帮助不多。总帐。顺便说一句:你得到了最好的 SO 名字,呵呵,这太棒了
    猜你喜欢
    • 2020-09-07
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    相关资源
    最近更新 更多