我正在编写一些代码来下载和处理电子邮件的附件,然后处理它们。该代码在某些情况下可以按要求工作,但仍然存在一些主要问题。
每当代码将附件加载到本地磁盘上的文件中时,都会花费很长时间,并且由于下载速度慢而经常超时,并出现以下异常:
A first chance exception of type 'Microsoft.Exchange.WebServices.Data.ServiceRequestException' occurred in Microsoft.Exchange.WebServices.dll
我可能错了,但是如果有问题的交换服务器与运行代码的服务器位于同一个千兆网络上,并且 Outlook 可以快速访问电子邮件、附件等,那么附件的下载速度应该比它们快得多,而且更加一致。以下是下载/加载时间的一些示例:
- 800KB 压缩 - 1m 4s
- 840KB 邮编 - 6m 18s
- 1.33MB 邮编 - 11m 23s
- 2.78MB 拉链 - 17m 3s
我尝试将 EWS 连接超时设置设置为 300000 毫秒,而不是默认的 100000 毫秒,以便为附件提供更多下载时间,并且异常数量略有减少,但现在等待时间太长了。
代码确实在线程中运行,一次不超过 8 个(我相信 10 个是 EWS 的节流限制),但我无法想象这会产生很大的不同。(当我一次测试单个电子邮件时,它还没有完成)。
这是下载附件的线程代码(为简单起见,删除了一些不相关的位):
Dim strMessageFolder As String
' Prepare the directory where this emails attachments will be stored
strMessageFolder = g_strFolder_Temp & strMessageID & "\"
' Create a folder to store the attachments for this email
Call FileSystem_CreateFolder(strMessageFolder, True)
' Process the emails attachments
For Each emailAttachment In emailMessage.Attachments
Dim fileattach As FileAttachment
'Dim fileattachStream As FileStream
Dim strAttachmentFile As String
' Prepare for the downloading of the attachment
fileattach = emailAttachment
blnTryFailed = False
intAttempts = 0
strAttachmentFile = strMessageFolder & fileattach.Name
' Handle up to 3 download attempts
Do
Try
' Try to download the attachment - Method 1
fileattach.Load(strAttachmentFile)
' Try to download the attachment - Method 2
'fileattachStream = New FileStream(strAttachmentFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)
'fileattach.Load(fileattachStream)
'fileattachStream.Close()
'fileattachStream.Dispose()
blnTryFailed = False
Catch ex As Exception
blnTryFailed = True
' Ensure the failed download is deleted
Call FileSystem_DeleteFile(strAttachmentFile)
intAttempts += 1
End Try
Loop While blnTryFailed And intAttempts < 3
' If the attachment download was unsuccessful then we cannot process the current email
If blnTryFailed = True Then
emailMessage.IsRead = False
'message.Subject = message.Subject & " - Attachment download failed, skipped"
Try
emailMessage.Update(ConflictResolutionMode.AutoResolve)
Catch ex As Exception
Call Logging_Add("Unable to mark email as skipped", strMessageID, "Debug")
End Try
Exit Sub
End If
如前所述,我知道 Exchange 限制,但找不到与下载附件速度相关的任何内容。所以我的问题是什么可能导致下载速度如此缓慢?