NullReferenceException from EWS Nuget

Viewed 1178

I am using Exchange.WebServices.Managed.Api 2.2.1.2 Nuget package to send email from EWS 2016. Sometimes I am getting exception given below. Email is sent out and saved to Sent Items folder as well, so looks like this is not a server issue and seems like somehow Nuget package is throwing this misleading error.

Has anyone encountered this?

    System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WrapStream(Stream responseStream, String contentEncoding)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetResponseStream(IEwsHttpWebResponse response)
   at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.ReadResponse(IEwsHttpWebResponse response)
   at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalCreateItems(IEnumerable`1 items, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode, ServiceErrorHandling errorHandling)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.CreateItem(Item item, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode)
   at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode)
   at Microsoft.Exchange.WebServices.Data.EmailMessage.InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition)
   at Microsoft.Exchange.WebServices.Data.EmailMessage.SendAndSaveCopy(WellKnownFolderName destinationFolderName)
   at Elliott.CollateralMgmt.Common.Email.Emailer.SendEws(Email email, Boolean isHtml, Dictionary`2 attachments) in /app/Elliott.CollateralMgmt.Common/Email/Emailer.cs:line 162
2 Answers

For debugging such issues I recommend that you download the sources from:

https://github.com/OfficeDev/ews-managed-api

You will then be able to add breakpoint and step into the Library code

I your specific problem, you should take a look at

Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WrapStream

I also encountered this with the nuget EWS 2.2.0 package. I found that I didn't hit this issue when referencing the git source https://github.com/OfficeDev/ews-managed-api instead of the nuget package for EWS 2.2.0 - depending on how you're using EWS, this might work for you.

Related