【发布时间】:2015-09-15 19:25:34
【问题描述】:
我有一项服务可以监视 2 个电子邮件收件箱。为每个收件箱创建一个线程。新邮件在启动时被读取并标记为已读,然后 NewMail 事件处理程序处理之后的所有新邮件。
我们一直在使用 EWS 版本 14。检索电子邮件没有问题。
在我们将电子邮件移至 Office 365 后,问题就出现了。
在启动时,现有新消息的检索和处理仍然正常。
启动后,当触发 NewMail 事件处理程序时,无论我们将 Timeout 属性设置为什么,bind/load 方法都会超时。
我们尝试切换到最新的 EWS 版本 (15.0)。
使用新版本,启动处理仍然可以正常工作。
但是,尝试在 NewMail 事件处理程序中绑定一封电子邮件不会出错,但会使收件箱线程退出。
当两个收件箱中都有新电子邮件时,整个服务在 Bind 行冻结(我让它等待 40 分钟,然后才不得不终止服务)。
我们尝试了以下两个 EWS 版本,但均未成功:
以下代码用于 NewEmail 事件处理程序和现有新消息的启动处理。为什么相同的代码会在一个地方而不是另一个地方出现问题?我该如何解决这个问题?
我也尝试通过 NotificationEventArgs 对象使用 EmailMessage.Bind,但我遇到了同样的问题。
我们尝试了 SyncLock 来绕过锁定和/或超时,但这没有帮助。 谢谢!
Private Sub HaveMail(ByVal sender As Object, ByVal e As Microsoft.Exchange.WebServices.Data.NotificationEventArgs)
Dim error_occured As Boolean = False
Try
Try
SyncLock lock
Logger.LogDebug("Just entered the Locked state of the HaveMail event")
Dim ir As FindItemsResults(Of Item) = service.FindItems(WellKnownFolderName.Inbox, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False), New ItemView(20))
Do While ir.Items.Count > 0
For Each i As EmailMessage In ir
Try
Logger.LogDebug("About to load a newly received email message")
i.Load(PropertySet.FirstClassProperties)
Catch ex As Exception
Throw New Exception("Error while loading an email message: " & ex.Message)
End Try
Dim rm As New ReceivedMessage
rm.ReceivedDateTime = i.DateTimeReceived
rm.ToAddress = i.DisplayTo
rm.Subject = i.Subject
rm.Body = i.Body
rm.FromAddress = i.From.Address
Logger.LogDebug("Just loaded a newly received email message from " & rm.FromAddress)
Dim rme As New ReceivedMessageEvent
rme.msg = rm
RaiseEvent GotMail(Me, rme)
Logger.LogDebug("Just processed a newly received email message from " & rm.FromAddress)
i.IsRead = True
i.Update(ConflictResolutionMode.AlwaysOverwrite)
Next
ir = service.FindItems(WellKnownFolderName.Inbox, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False), New ItemView(20))
Loop
End SyncLock
【问题讨论】:
标签: vb.net multithreading exchangewebservices