C# · 12月 21, 2021

EWS 流通知订阅邮件

摘要

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

核心代码块<span style=”color: #0000ff;”>namespace<span style=”color: #000000;”> Exchange101
{
<span style=”color: #008000;”>//<span style=”color: #008000;”> This sample is for demonstration purposes only. Before you run this sample,make sure that the code meets the coding requirements of your organization.
<span style=”color: #0000ff;”>class<span style=”color: #000000;”> Notifications
{
<span style=”color: #0000ff;”>static ExchangeService service = Service.ConnectToService(UserDataFromConsole.GetUserData(),<span style=”color: #0000ff;”>new<span style=”color: #000000;”> TraceListener());
<span style=”color: #0000ff;”>private <span style=”color: #0000ff;”>static<span style=”color: #000000;”> AutoResetEvent Signal;

</span><span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Main(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt;[] args) { SetStreamingNotifications(service); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Wait for the application to exit</span> Signal = <span style=”color: #0000ff;”&gt;new</span> AutoResetEvent(<span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;); Signal.WaitOne(); } </span><span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; SetStreamingNotifications(ExchangeService service) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Subscribe to streaming notifications on the In@R_757_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@ folder,and listen </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; for “NewMail”,”Created”,and “Deleted” events. </span> StreamingSubscription streamingsubscription =<span style=”color: #000000;”&gt; service.SubscribeToStreamingNotifications( </span><span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; FolderId[] { WellK<a href=”https://www.jb51.cc/tag/Now/” target=”_blank” class=”keywords”>Now</a>nFolderName.In@R_757_2<a href=”https://www.jb51.cc/tag/419/” target=”_blank” class=”keywords”>419</a>@ },EventType.NewMail,EventType.Created,EventType.Deleted); StreamingSubscriptionConnection connection </span>= <span style=”color: #0000ff;”&gt;new</span> StreamingSubscriptionConnection(service,<span style=”color: #800080;”&gt;1</span><span style=”color: #000000;”&gt;); connection.AddSubscription(streamingsubscription); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Delegate event handlers. </span> connection.OnNotificationEvent += <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent); connection.OnSubscriptionError </span>+= <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError); connection.OnDisconnect </span>+= <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect); connection.<a href=”https://www.jb51.cc/tag/open/” target=”_blank” class=”keywords”>open()</a>; Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;——— StreamSubscription event ——-</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); } </span><span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;private</span> <span style=”color: #0000ff;”&gt;void</span> OnDisconnect(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,SubscriptionErrorEventArgs args) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Cast the sender as a StreamingSubscriptionConnection object. </span> StreamingSubscriptionConnection connection =<span style=”color: #000000;”&gt; (StreamingSubscriptionConnection)sender; </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Ask the user if they want to reconnect or close the subscription. </span>

<span style=”color: #000000;”> ConsoleKeyInfo cki;
Console.WriteLine(<span style=”color: #800000;”>”<span style=”color: #800000;”>The connection to the subscription is disconnected.<span style=”color: #800000;”>”<span style=”color: #000000;”>);
Console.WriteLine(<span style=”color: #800000;”>”<span style=”color: #800000;”>Do you want to reconnect to the subscription? Y/N<span style=”color: #800000;”>”<span style=”color: #000000;”>);
<span style=”color: #0000ff;”>while (<span style=”color: #0000ff;”>true<span style=”color: #000000;”>)
{
cki = Console.ReadKey(<span style=”color: #0000ff;”>true<span style=”color: #000000;”>);
{
<span style=”color: #0000ff;”>if (cki.Key ==<span style=”color: #000000;”> ConsoleKey.Y)
{
connection.open();
Console.WriteLine(<span style=”color: #800000;”>”<span style=”color: #800000;”>Connection open.<span style=”color: #800000;”>”<span style=”color: #000000;”>);
Console.WriteLine(<span style=”color: #800000;”>”<span style=”color: #800000;”>\r\n<span style=”color: #800000;”>”<span style=”color: #000000;”>);
<span style=”color: #0000ff;”>break<span style=”color: #000000;”>;
}
<span style=”color: #0000ff;”>else <span style=”color: #0000ff;”>if (cki.Key ==<span style=”color: #000000;”> ConsoleKey.N)
{
Signal.Set();
<span style=”color: #0000ff;”>bool isOpen =<span style=”color: #000000;”> connection.IsOpen;

</span><span style=”color: #0000ff;”&gt;if</span> (isOpen == <span style=”color: #0000ff;”&gt;true</span><span style=”color: #000000;”&gt;) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Close the connection</span>

<span style=”color: #000000;”> connection.Close();
}
<span style=”color: #0000ff;”>else<span style=”color: #000000;”>
{
<span style=”color: #0000ff;”>break<span style=”color: #000000;”>;
}
}
}
}

} </span><span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> OnEvent(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,NotificationEventArgs args) { StreamingSubscription subscription </span>=<span style=”color: #000000;”&gt; args.Subscription; </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Loop through all item-related events. </span> <span style=”color: #0000ff;”&gt;foreach</span> (NotificationEvent notification <span style=”color: #0000ff;”&gt;in</span><span style=”color: #000000;”&gt; args.Events) { </span><span style=”color: #0000ff;”&gt;switch</span><span style=”color: #000000;”&gt; (notification.EventType) { </span><span style=”color: #0000ff;”&gt;case</span><span style=”color: #000000;”&gt; EventType.NewMail: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;\n————-Mail created:————-</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); </span><span style=”color: #0000ff;”&gt;break</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;case</span><span style=”color: #000000;”&gt; EventType.Created: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;\n————-Item or folder created:————-</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); </span><span style=”color: #0000ff;”&gt;break</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;case</span><span style=”color: #000000;”&gt; EventType.Deleted: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;\n————-Item or folder deleted:————-</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); </span><span style=”color: #0000ff;”&gt;break</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Display the notification identifier. </span> <span style=”color: #0000ff;”&gt;if</span> (notification <span style=”color: #0000ff;”&gt;is</span><span style=”color: #000000;”&gt; ItemEvent) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; The NotificationEvent for an email message is an ItemEvent. </span> ItemEvent itemEvent =<span style=”color: #000000;”&gt; (ItemEvent)notification; Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;\nItemId: </span><span style=”color: #800000;”&gt;”</span> +<span style=”color: #000000;”&gt; itemEvent.ItemId.UniqueId); } </span><span style=”color: #0000ff;”&gt;else</span><span style=”color: #000000;”&gt; { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; The NotificationEvent for a folder is a FolderEvent. </span> FolderEvent folderEvent =<span style=”color: #000000;”&gt; (FolderEvent)notification; Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;\nFolderId: </span><span style=”color: #800000;”&gt;”</span> +<span style=”color: #000000;”&gt; folderEvent.FolderId.UniqueId); } } } </span><span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> OnError(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,SubscriptionErrorEventArgs args) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt; Handle error conditions. </span> Exception e =<span style=”color: #000000;”&gt; args.Exception; Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;\n————-Error —</span><span style=”color: #800000;”&gt;”</span> + e.Message + <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;————-</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); } }

}

相关资料