EWS - 通过流媒体订阅获得O365通知信息并处理。
最近有个工作上的需求,需要对具有一定标识的邮件进行自动化处理
对Exchange的一些订阅研究后发现,微软推荐使用流媒体订阅的方式来处理新邮件通知,在微软网站上找了一些信息和代码示例,先做了一个简单的版本如下:
static void Main(){
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1){
Url = "",
Credentials = new WebCredentials(username, password),
};
SetStreamingNotification(service);
}
private void SetStreamingNotification(ExchangeService service){
StreamingSubscription subscription = service.SubscribeToStreamingNotifications(
new FolderId[]{
WellKnownFolderName.Inbox,
}
EventType.NewMail);
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
connection.AddSubscription(subscription);
connection.OnNotificationEvent += OnNotification;
connection.OnSubscriptionError += OnSubscriptionError;
connection.OnDisconnect += OnDisconnect;
connection.Open(