C# · 12月 21, 2021

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

摘要

在ews中提供了一种拉通知的方式获取服务端邮件的操作,通过这种方式客户端可以订阅该通知,在邮箱有变化的时候,可以收到通知。

工作方式

EWS 通知是在订阅的基础上处理的。通常是一个订阅每个邮箱,以及邮箱订阅中您可以订阅部分或全部文件夹。您决定何种通知订阅 (流、 请求或推) 以及哪种类型的事件您想要接收 (NewMail,创建、 删除、 修改日期,等等),然后创建订阅。EWS 事件然后发送以异步方式从邮箱服务器到客户端。

<p class="subheading">EWS 流式处理通知

<p class="subheading">EWS 拉通知

一个例子

ExchangeService service = Main(= +== </span><span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> mytimer_Elapsed(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,ElapsedEventArgs e) { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;running</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); GetPullNotifications(); } </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; SubscribePullNotifications(FolderId folderId) { service.Url </span>= <span style=”color: #0000ff;”&gt;new</span> Uri(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;https://mail.testsssss.com/ews/exchange.asmx</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); service.Credentials </span>= <span style=”color: #0000ff;”&gt;new</span> NetworkCredential(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;www</span><span style=”color: #800000;”&gt;”</span>,<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;12345</span><span style=”color: #800000;”&gt;”</span>,<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;domain</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); Subscription </span>= service.SubscribeToPullNotifications(<span style=”color: #0000ff;”&gt;new</span> FolderId[] { folderId },<span style=”color: #800080;”&gt;1440</span>,<span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;,EventType.NewMail,EventType.Created,EventType.Deleted); } </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; GetPullNotifications() { IEnumerable</span><ItemEvent> itemEvents =<span style=”color: #000000;”&gt; Subscription.GetEvents().ItemEvents; </span><span style=”color: #0000ff;”&gt;foreach</span> (ItemEvent itemEvent <span style=”color: #0000ff;”&gt;in</span><span style=”color: #000000;”&gt; itemEvents) { </span><span style=”color: #0000ff;”&gt;switch</span><span style=”color: #000000;”&gt; (itemEvent.EventType) { </span><span style=”color: #0000ff;”&gt;case</span><span style=”color: #000000;”&gt; EventType.Copied: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;Copied</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;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;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: #0000ff;”&gt;case</span><span style=”color: #000000;”&gt; EventType.FreeBusyChanged: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;FreeBusyChanged</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.Modified: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;Modified</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.Moved: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;Moved</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.NewMail: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;New mail</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); Console.WriteLine(itemEvent.ItemId.UniqueId); EmailMessage emailMessage </span>=<span style=”color: #000000;”&gt; EmailMessage.Bind(service,itemEvent.ItemId.UniqueId); Console.WriteLine(emailMessage.Subject); Console.WriteLine(emailMessage.Body.Text); Console.WriteLine(emailMessage.From); Console.WriteLine(emailMessage.DateTimeReceived); Console.WriteLine(</span><span style=”color: #0000ff;”&gt;string</span>.Join(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;;</span><span style=”color: #800000;”&gt;”</span>,emailMessage.ToRecipients.Select(x =><span style=”color: #000000;”&gt; x.Address).ToArray())); </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.Status: Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;Status</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;default</span><span style=”color: #000000;”&gt;: </span><span style=”color: #0000ff;”&gt;break</span><span style=”color: #000000;”&gt;; } } }}

}

开始监听

当收到新的邮件时,eventType为NewMail,可以根据uniqueid查询当前邮件详情。

参考资料