博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EWS 流通知订阅邮件
阅读量:6278 次
发布时间:2019-06-22

本文共 5319 字,大约阅读时间需要 17 分钟。

摘要

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

核心代码块

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("\r\n");                        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("\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 email message is an ItemEvent.                     ItemEvent itemEvent = (ItemEvent)notification;                    Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);                }                else                {                    // The NotificationEvent for a folder is a FolderEvent.                     FolderEvent folderEvent = (FolderEvent)notification;                    Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);                }            }        }        static void OnError(object sender, SubscriptionErrorEventArgs args)        {            // Handle error conditions.             Exception e = args.Exception;            Console.WriteLine("\n-------------Error ---" + e.Message + "-------------");        }     }}

相关资料

转载地址:http://qzfva.baihongyu.com/

你可能感兴趣的文章
Lua string.gsub (s, pattern, repl [, n])
查看>>
智能聊天机器人实现(源代码+解析)
查看>>
微表面分布函数(Microfacet Distribution Function)确切含义
查看>>
轻松python文本专题-字符与字符值转换
查看>>
JAVA-MyEclipse第一个实例
查看>>
iOS 9 学习系列: Xcode Code Coverage
查看>>
休眠模式的开关闭
查看>>
Variable number of arguments (Varargs)
查看>>
jquery.ajax之beforeSend方法使用介绍
查看>>
usb键鼠驱动分析【钻】
查看>>
shell中while循环的陷阱
查看>>
Java 系书籍,,,,,,,,,,,,,
查看>>
binlog 轻松的找到没有及时提交的事物(infobin工具
查看>>
windows下如何创建没有名字的.htaccess文件
查看>>
关于Unity中物体分别在本地和世界坐标系对应方向的移动
查看>>
引用外部jquery.js
查看>>
WebLogic 11g重置用户密码
查看>>
Python sin() 函数
查看>>
Python isalnum() 方法
查看>>
Nginx负载均衡器处理Session共享的几种方法(转)
查看>>