[EWS]如何: 通过使用 Exchange 中的 EWS 流有关邮箱事件的通知
摘要
在之前的文章中,介绍ews拉通知的模式订阅邮件。详情可阅读这篇文章:EWS 通过SubscribeToPullNotifications订阅Exchange新邮件提醒 ,可以看到拉通知的模式,是一次订阅,然后定时器不停的请求服务器。
流通知
流式处理通知依赖悬挂 get 请求在服务器上保留流订阅连接以便连接处于活动状态时所发生的任何事件传送到客户端立即打开。过程中的一个连接,并连接保持打开直到间隔过期,或最多 30 分钟可以发送多个通知。连接到期后,客户端发送悬挂再次 get 请求。图 2 显示了流订阅和流式处理通知的工作方式。
c#代码
// Subscribe to streaming notifications in the Inbox. StreamingSubscription = service.SubscribeToStreamingNotifications( new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail, EventType.Created, EventType.Deleted, EventType.Modified, EventType.Moved, EventType.Copied, EventType.FreeBusyChanged); // 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(StreamingSubscription); connection.OnNotificationEvent += OnNotificationEvent; connection.OnDisconnect += OnDisconnect; connection.Open();
可以通过上面代码看到,可以流通知方式,可以保持长连接,监听30分钟内收件箱邮件的事件,如果超时,可以出发断开连接的事件。这时我们可以再次创建连接。
获取邮件内容
private void OnNotificationEvent(object sender, NotificationEventArgs args) { if (args != null) { foreach (NotificationEvent notificationEvent in args.Events) { if (notificationEvent is ItemEvent) { ItemEvent itemEvent = (ItemEvent)notificationEvent; Console.WriteLine(notificationEvent.EventType.ToString()); Item item = Item.Bind(this._service, itemEvent.ItemId); switch (notificationEvent.EventType) { case EventType.Moved: Console.WriteLine(item.Subject); break; case EventType.NewMail: Console.WriteLine(item.Subject); break; default: break; } } } } }
参考文章
流通知
https://msdn.microsoft.com/zh-cn/library/office/dn458792(v=exchg.150).aspx
三种通知模式
https://msdn.microsoft.com/zh-cn/library/office/dn458791(v=exchg.150).aspx
-
博客地址: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-20 [css]通过transform缩放邮件客户端h5页面
2015-09-20 [实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册
2013-09-20 [Asp.Net]状态管理(ViewState、Cookie)