zoukankan      html  css  js  c++  java
  • EWS 流通知订阅邮件

    摘要

    查找一些关于流通知订阅邮件的资料,这里整理一下。

    核心代码块

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Timers;
    using Exchange101;
    using Microsoft.Exchange.WebServices.Data;
    
    namespace Exchange101
    {
        // This sample is for demonstration purposes only. Before you run this sample, make sure that the code meets the coding requirements of your organization.    
        class Notifications
        {
            static ExchangeService service = Service.ConnectToService(UserDataFromConsole.GetUserData(), new TraceListener());
            private static AutoResetEvent Signal; 
    
    
            static void Main(string[] args)
            {
                SetStreamingNotifications(service);
    
                // Wait for the application to exit
                Signal = new AutoResetEvent(false);
                Signal.WaitOne();
    
            }
            static void SetStreamingNotifications(ExchangeService service)
            {
                // Subscribe to streaming notifications on the Inbox folder, and listen 
                // for "NewMail", "Created", and "Deleted" events. 
                StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(
                    new FolderId[] { WellKnownFolderName.Inbox },
                    EventType.NewMail,
                    EventType.Created,
                    EventType.Deleted);
    
                StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 1);
    
                connection.AddSubscription(streamingsubscription);
                // Delegate event handlers. 
                connection.OnNotificationEvent +=
                    new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
                connection.OnSubscriptionError +=
                    new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
                connection.OnDisconnect +=
                    new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
                connection.Open();
    
                Console.WriteLine("--------- StreamSubscription event -------");
            }
    
            static private 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.");
                            Console.WriteLine("
    ");
                            break;
                        }
                        else if (cki.Key == ConsoleKey.N)
                        {
                            Signal.Set();
                            bool isOpen = connection.IsOpen;
                            
                            if (isOpen == true)
                            {
                            // Close the connection
                            connection.Close();
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
    
            }
    
            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("
    -------------Mail created:-------------");
                            break;
                        case EventType.Created:
                            Console.WriteLine("
    -------------Item or folder created:-------------");
                            break;
                        case EventType.Deleted:
                            Console.WriteLine("
    -------------Item or folder deleted:-------------");
                            break;
                    }
                    // Display the notification identifier. 
                    if (notification is ItemEvent)
                    {
                        // The NotificationEvent for an email message is an ItemEvent. 
                        ItemEvent itemEvent = (ItemEvent)notification;
                        Console.WriteLine("
    ItemId: " + itemEvent.ItemId.UniqueId);
                    }
                    else
                    {
                        // The NotificationEvent for a folder is a FolderEvent. 
                        FolderEvent folderEvent = (FolderEvent)notification;
                        Console.WriteLine("
    FolderId: " + folderEvent.FolderId.UniqueId);
                    }
                }
            }
            static void OnError(object sender, SubscriptionErrorEventArgs args)
            {
                // Handle error conditions. 
                Exception e = args.Exception;
                Console.WriteLine("
    -------------Error ---" + e.Message + "-------------");
            } 
    
        }
    }

    相关资料

    https://msdn.microsoft.com/en-us/library/office/dn458791(v=exchg.150).aspx

    https://blogs.msdn.microsoft.com/emeamsgdev/2013/04/16/ews-streaming-notification-sample/

  • 相关阅读:
    jQuery知识总结
    WEB架构师成长之路之2
    利用Travis CI 让你的github项目持续构建(Node.js为例)
    C#实现UDP分包组包
    win7下安装32位GoSublime Package
    爬虫部分技术要点浅析
    如何使用“依赖注入”的
    分布式ACM Online Judge 架构设计
    “容器组件服务”模型
    Maven学习
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/7580198.html
Copyright © 2011-2022 走看看