摘要

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

核心代码块

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Timers;
  7. using Exchange101;
  8. using Microsoft.Exchange.WebServices.Data;
  9.  
  10. namespace Exchange101
  11. {
  12. // This sample is for demonstration purposes only. Before you run this sample, make sure that the code meets the coding requirements of your organization.
  13. class Notifications
  14. {
  15. static ExchangeService service = Service.ConnectToService(UserDataFromConsole.GetUserData(), new TraceListener());
  16. private static AutoResetEvent Signal;
  17.  
  18. static void Main(string[] args)
  19. {
  20. SetStreamingNotifications(service);
  21.  
  22. // Wait for the application to exit
  23. Signal = new AutoResetEvent(false);
  24. Signal.WaitOne();
  25.  
  26. }
  27. static void SetStreamingNotifications(ExchangeService service)
  28. {
  29. // Subscribe to streaming notifications on the Inbox folder, and listen
  30. // for "NewMail", "Created", and "Deleted" events.
  31. StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(
  32. new FolderId[] { WellKnownFolderName.Inbox },
  33. EventType.NewMail,
  34. EventType.Created,
  35. EventType.Deleted);
  36.  
  37. StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, );
  38.  
  39. connection.AddSubscription(streamingsubscription);
  40. // Delegate event handlers.
  41. connection.OnNotificationEvent +=
  42. new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
  43. connection.OnSubscriptionError +=
  44. new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
  45. connection.OnDisconnect +=
  46. new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
  47. connection.Open();
  48.  
  49. Console.WriteLine("--------- StreamSubscription event -------");
  50. }
  51.  
  52. static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
  53. {
  54. // Cast the sender as a StreamingSubscriptionConnection object.
  55. StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
  56. // Ask the user if they want to reconnect or close the subscription.
  57. ConsoleKeyInfo cki;
  58. Console.WriteLine("The connection to the subscription is disconnected.");
  59. Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
  60. while (true)
  61. {
  62. cki = Console.ReadKey(true);
  63. {
  64. if (cki.Key == ConsoleKey.Y)
  65. {
  66. connection.Open();
  67. Console.WriteLine("Connection open.");
  68. Console.WriteLine("\r\n");
  69. break;
  70. }
  71. else if (cki.Key == ConsoleKey.N)
  72. {
  73. Signal.Set();
  74. bool isOpen = connection.IsOpen;
  75.  
  76. if (isOpen == true)
  77. {
  78. // Close the connection
  79. connection.Close();
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. }
  86. }
  87. }
  88.  
  89. }
  90.  
  91. static void OnEvent(object sender, NotificationEventArgs args)
  92. {
  93. StreamingSubscription subscription = args.Subscription;
  94.  
  95. // Loop through all item-related events.
  96. foreach (NotificationEvent notification in args.Events)
  97. {
  98.  
  99. switch (notification.EventType)
  100. {
  101. case EventType.NewMail:
  102. Console.WriteLine("\n-------------Mail created:-------------");
  103. break;
  104. case EventType.Created:
  105. Console.WriteLine("\n-------------Item or folder created:-------------");
  106. break;
  107. case EventType.Deleted:
  108. Console.WriteLine("\n-------------Item or folder deleted:-------------");
  109. break;
  110. }
  111. // Display the notification identifier.
  112. if (notification is ItemEvent)
  113. {
  114. // The NotificationEvent for an email message is an ItemEvent.
  115. ItemEvent itemEvent = (ItemEvent)notification;
  116. Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
  117. }
  118. else
  119. {
  120. // The NotificationEvent for a folder is a FolderEvent.
  121. FolderEvent folderEvent = (FolderEvent)notification;
  122. Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
  123. }
  124. }
  125. }
  126. static void OnError(object sender, SubscriptionErrorEventArgs args)
  127. {
  128. // Handle error conditions.
  129. Exception e = args.Exception;
  130. Console.WriteLine("\n-------------Error ---" + e.Message + "-------------");
  131. }
  132.  
  133. }
  134. }

相关资料

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/

EWS 流通知订阅邮件的更多相关文章

  1. EWS 通过SubscribeToPullNotifications订阅Exchange新邮件提醒

    摘要 在ews中提供了一种拉通知的方式获取服务端邮件的操作,通过这种方式客户端可以订阅该通知,在邮箱有变化的时候,可以收到通知. 工作方式 EWS 通知是在订阅的基础上处理的.通常是一个订阅每个邮箱, ...

  2. [EWS]如何: 通过使用 Exchange 中的 EWS 流有关邮箱事件的通知

    摘要 在之前的文章中,介绍ews拉通知的模式订阅邮件.详情可阅读这篇文章:EWS 通过SubscribeToPullNotifications订阅Exchange新邮件提醒 ,可以看到拉通知的模式,是 ...

  3. EWS 通过SubscribeToPullNotifications订阅Exchange删除邮件

    摘要 在使用拉通知的方式监听exchange邮件的时候,无法监听到收件箱删除的邮件.最后通过调试发现,在删除收件箱邮件的时候,是将收件箱的邮件移动到了deleted item文件夹,会触发Moved事 ...

  4. Dynamic CRM 2013学习笔记(三十四)自定义审批流5 - 自动邮件通知

    审批过程中,经常要求自动发邮件:审批中要通知下一个审批人进行审批:审批完通知申请人已审批完:被拒绝后,要通知已批准的人和申请人.下面详细介绍如何实现一个自动发邮件的插件:   1. 根据审批状态来确定 ...

  5. SignalR主动通知订阅者示例

    html代码: <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script> <scri ...

  6. EWS 邮件提醒

    摘要 之前做的邮件提醒的项目,最近需要优化,由于使用了队列,但即时性不是特别好,有队列,就会出现先后的问题,最近调研了exchange 流通知的模式,所以想使用流通知模式和原先的拉取邮件的方法结合,在 ...

  7. 订阅Linux内核的邮件列表

    1.发送订阅邮件 注意:一定不要使用像Foxmail这样的第三方邮件客户端,因为发送的邮件会转码,导致订阅不成功,推荐使用Gmail,直接登录进去发送. 发送邮件内容: 接收人:majordomo@v ...

  8. 使用TheFolderSpy监控文件夹的变化-邮件通知

    一.概述 当我们的文档或者代码文件发布在公网.共享文件夹中,其他用户具备访问或修改的权限时,就存在文档被覆盖或删除的分享.另外一个典型的场景,发布在Web服务器上的网页文件,在网站版本不更新的时间,服 ...

  9. 微软BI 之SSRS 系列 - 报表邮件订阅中 SMTP 服务器匿名访问与 Windows验证, 以及如何成功订阅报表的实例

    这篇文章源于在上一篇博文中有园友提出订阅 SSRS 报表时的一个问题,  于是就好好总结了一下,把有关 SSRS 报表订阅的要点和容易出现问题的地方写出来,希望对大家有所帮助! 参看上一篇博文 - S ...

随机推荐

  1. C# 动态创建SQL数据库(一)

    最近在做项目中要求能够要求动态添加数据库并建表.具体思路如下 1 提供数据名,根据数据库创建数据库 2 自定数据库与数据表,提供数据表自定与数据类型创建表 创建sqlhelper类,用于数据库操作 u ...

  2. 探究Entity Framework如何在多个仓储层实例之间工作单元的实现及原理(2018-05-31修改部分严重错误代码)

    前言 1.本文的前提条件:EF上下文是线程唯一,EF版本6.1.3. 2.网上已有相关API的详细介绍,本文更多的是作为我自己的个人学习研究记录. 3.2018-05-31修改DbSession.cs ...

  3. CF1146H Satanic Panic

    题目传送门 Description 给定二维平面内\(n\)个点\((n\leq 300)\),求能组成五角星(不要求正五角星)的五元组个数. Solution 一道小清新的寄蒜几盒计算几何题,代码不 ...

  4. Python3.5 学习二十四

    本节课程大纲: -------------------------------------------------------------------------------------------- ...

  5. 2018 Multi-University Training Contest 6

    A.oval-and-rectangle 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6362 题意:在长半轴为a,短半轴为b的椭圆内部,以y=c( ...

  6. Java代码审计连载之—SQL注入

    前言近日闲来无事,快两年都没怎么写代码了,打算写几行代码,做代码审计一年了,每天看代码都好几万行,突然发现自己都不会写代码了,真是很DT.想当初入门代码审计的时候真是非常难,网上几乎找不到什么java ...

  7. underscore.js源码研究(4)

    概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...

  8. solr初识

    参考资料http://blog.csdn.net/l1028386804/article/details/70199983

  9. Hadoop 2.4.1+HBase 0.98.6.1 分布式安装

    参考:http://blog.csdn.net/wind520/article/details/39856353

  10. [Fatal Error] :3:13: Open quote is expected for attribute "{1}" associated with an element type "id".

    用DOM解析XML时出现了如下错误: [Fatal Error] :3:13: Open quote is expected for attribute "{1}" associa ...