I'm in the process of writing some code to download and process the attachments of email messages and then process them. The code is working as required in some cases, but still has some major problems.
Whenever the code loads the attachment into a file on the local disk, it takes a very long time to do so and often times out with the following exception as a result of the slow download:
A first chance exception of type 'Microsoft.Exchange.WebServices.Data.ServiceRequestException' occurred in Microsoft.Exchange.WebServices.dll
I may be wrong, but if the exchange server in question is on the same gigabit network as the server running the code and outlook has fast access to emails, attachments, etc then attachments should download considerably faster than they do and much more consistently. Here are some example of download/load times:
- 800KB Zip - 1m 4s
- 840KB Zip - 6m 18s
- 1.33MB Zip - 11m 23s
- 2.78MB Zip - 17m 3s
I have tried setting the EWS connection timeout setting to 300000ms instead of the default 100000ms to give the attachments more time to download and the number of exceptions has decreased slightly but the waiting time is now way too long.
The code does run in threads, no more than 8 at a time (10 being the throttling limit for EWS i believe), but i cant imagine that would make much of a difference. (It hasn't done when I've been testing single emails at a time).
Here is the threaded code that downloads the attachments (some un-related bits removed for simplicity):
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
As mentioned previously, im aware of the Exchange throttling but cannot find anything related to the speed at which attachments are downloaded. So my question is what could possibly be causing such slow download speeds?
I have the same issue with my app. The problem was caused by default EWS setting which writes to Console all HttpRequest and HttpResponse messages between EWS and application. Turning off TraceFlags was blessing. My code in c#:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.TraceFlags = TraceFlags.None;
来源:https://stackoverflow.com/questions/10091745/exchange-web-services-attachment-load-is-slow