I am attempting to send a message with a PDF attachment using the EWS Managed API 2.0. I am sending email as an account that is not my own but for which I have send as permissions.
I can send the email without an attachment, but as soon as I try to send the attachment the request fails.
The file definitely exists.
I have implemented the TraceListener
and see that the Create Attachment request is sent when SendAndSaveCopy
is called but I do not receive a proper response from the server (I do know the server is getting my request though as the error is clearly from the server). I do not see a request to send the email message after the create attachment appears to fail.
The error I receive upon trying to SendAndSaveCopy
is The request failed. The underlying connection was closed: An unexpected error occurred on a send.
The inner exception is Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
I have googled this and according to what few suggestions I've found, I've confirmed this is not simply a timeout of the subscription (supported by the fact in the exact same context I can SendAndSaveCopy just fine without an attachment, and also by the fact I can send an error email after the failure just fine). Others have cited issues with file size, but my file size is very small (151 KB).
My Exchange administrator is checking to see if there is a setting on the server side that could be affecting this but hasn't found anything yet.
Can anyone tell me if they have encountered (and found a resolution to) this particular problem? Even any tips about the particular settings I could direct my Exchange administrator to look at?
My code is attached below (I've removed my gobs of error message printing for readability):
Public Function SendEmailResponse(ByVal strSender As String, ByVal strRecipient As String, ByVal strSubject As String, ByVal strBody As String, _
ByVal ews2010 As ExchangeService, Optional ByVal strCCAddresses As List(Of String) = Nothing, _
Optional ByVal strFilesToAttach As List(Of String) = Nothing, _
Optional ByVal blnReceipt As Boolean = False) As Boolean
Try
Dim msgReply As New EmailMessage(ews2010)
msgReply.Subject = strSubject
msgReply.Body = New MessageBody(BodyType.Text, strBody)
Dim fromAddress As New EmailAddress(strSender)
msgReply.From = fromAddress
msgReply.ToRecipients.Add(strRecipient)
msgReply.IsReadReceiptRequested = blnReceipt
If strCCAddresses IsNot Nothing Then
For Each strCC As String In strCCAddresses
msgReply.CcRecipients.Add(strCC)
Next
End If
msgReply.Save() '''This works just fine
If strFilesToAttach IsNot Nothing Then
For Each flAttach In strFilesToAttach
msgReply.Attachments.AddFileAttachment(flAttach)
Next
End If
msgReply.SendAndSaveCopy() '''CRASHES HERE IF AND ONLY IF I've attached files in the above loop
SendEmailResponse = True
Catch ex As Exception
SendEmailResponse = False
End Try
End Function