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("\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 + "-------------"); } } }
相关资料
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/
-
博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2016-09-23 [Bug]IIs Cannot read configuration file due to insufficient permissions