EWSClient throws a Object reference not set to an instance of an object exception

Hello,


We’re using Aspose.Email version 6.3 for the .NET framework.
When we try to instantiate a new IEWSClient using this:
IEWSClient client = EWSClient.GetEWSClient(“serveradddress”, “username”, “password”);
Get get the generic “Object reference not set to an instance of an object” exception.
We tested with several Exchange 2013 servers and the error is the same.

Are you able to create an EWS client using Exchange 2013?
I’m wondering if there is a bug in the Aspose.Email (v 6.3) library or if Microsoft made some changes to Exchange and causes this to throw an exception.

Thanks!

Hi Corneliu.

Thank you for writing to Aspose Support team.

We are not able to face any such issue as you have specified and are able to connect to the Office365 Exchange server 2013 without any issue. Please try the following cdoe sample with the test account credentials and let us know your feedback. Also, please use the latest version of the API i.e. Aspose.Email for .NET 6.8.0 for testing at your end.

Sample Code:

const string mailboxUri = “[https://outlook.office365.com/ews/exchange.asmx ](https://outlook.office365.com/ews/exchange.asmx)”;

const string domain = @"";

const string username = "userone@AsposeAug2016.onmicrosoft.com";

<span style=“font-family: “Courier New”; font-size: 13.44px;”>const string password = @“Aspose1234”;

NetworkCredential credential = new NetworkCredential(username, password, domain);

IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credential);

Console.WriteLine(client.ListMessages(client.MailboxInfo.InboxUri).Count);

Hello Kashif Iqbal,


Thanks for providing the above information to test with EWS. I was able to connect to that exchange server using EWS. Could you also try to connect to an Exchange Server that’s hosted on premises instead of being hosted by Microsoft?
In my case, we are hosting our own Exchange Server and when I try to connect to it I get the error I mentioned earlier. When I try to connect to the Exchange Server you provided, it works fine.

Thanks!

Hi Corneliu,


We have tested the connectivity to Exchange 2013 on a locally installed instance and it doesn’t raise any problem as you have mentioned. Could you please share more details about the error you are getting in terms of stack trace? Also, if it is possible for you to create a test account on your exchange server and share its credentials with us? We’ll try to connect to it from our end for further investigation of this issue.

Currently I can't provide a test account since it's the customer's Exchange Server, but I will try to request one from them.

Today I noticed another strange thing when trying to connect using EWS.
Try these information when connecting:

Server Address: https://abc.google.com/test/Exchange.asmx
Username: test
Password: test

I know that's not a valid Exchange Server (or at least it's not running) but I still get a "Object reference not set to an instance of an object" (try it).
Is there a reason why we get this generic exception when trying to connect to an Exchange Server that is either down or not responding?
A much better message would be something similar to "Could not reach the Exchange Server" or something of that nature.

Hi,

Could you please share with us which version of the API are you using at your end? We have tested your stance at our end with the latest version of the API i.e. Aspose.Email for .NET 6.8.0, but were not able to reproduce the problem at our end. We get proper exception in this case which states that "The remote name could not be resolved: abc.google.com as shown in the attached screenshot. Please try it at your end with the latest version of the API and let us know your feedback.

Thank you for the screenshot and testing that.

On our end we have a NT Service that’s running as Local System. The service uses Aspose’s library to connect to the Exchange Server and retrieve those emails and store them somewhere on the machine.
I discovered that if we run the service as Local System, we get the “Object reference…” exception when trying to connect to the Exchange Server. If we run the service as myself (or anyone that’s an actual account), then we are able to connect to the Exchange Server using Aspose.
Can you please try this scenario on your end? Create an NT/Window Service that uses Aspose to connect to the Exchange Server? And make sure the service runs as Local System account?

Thanks a lot!

Hi Corneliu,


We have tested the issue as myself (with my user account on windows 7 machine) and didn’t face any problems. Could you please share a simple NT/Windows Service application project with us and guide us for executing it as a local service? We’ll look into it for assisting you further.

Try to run the NT Service as Local System account NOT as yourself/your account. I believe that’s the problem. Please try that and if you still can’t reproduce it I will create a sample project

Finally found out the root cause of the problem.


Using Aspose:
If the certificate of the Exchange Server is invalid (for any reason), then method EWSClient.GetEWSClient throws an exception with the reason “Object reference not set to an instance of an object”.

Using Microsoft’s API for EWS:
If the certificat is invalid, we get this error message: “The request failed. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.” when trying to connect.

So it’s not an Exchange, Aspose, Service account problem. It’s a certificate problem. Though I strongly believe that Aspose’s error message should be something related to the certificate/TLS/etc, instead of the generic “Object reference…” error message. This can be misleading and cause a lot of unnecessary work.
For example, if the customer contacted us with the error message similar to what Microsoft’s API for EWS shows, then we would know directly that it’s a certificate error. Because of the generic message received from Aspose, we had to debug the problem for several hours to find out that it is a certificate error.




Hi Corneliu,


We are glad to know that your issue has been resolved. With the latest version of the API i.e. Aspose.Email for .NET 6.8.0, we get “The remote certificate is invalid according to the validation procedure” error when trying with an invalid certificate. It seems you are using some old version of the API that may have such a different error message. Anyways, please feel free to write to us if you face the same exception with the latest version of the API for our reference.

That’s good to know.

We’ll upgrade to the latest API sometime soon and we’ll test this particular scenario.
Thank you for taking the time to look into this situation!

You are welcome.