Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET CORE and EWS Managed API connection problem

From our intranet web app (Asp.net core 3) I would like to add the ability for users to send emails. We have an on premises MS Exchange Server 2016 (part of our domain).

Following this example from Microsoft: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications I have put together the following code to send a test email:

//exchange
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.UseDefaultCredentials = true;
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("[email protected]");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
email.Send();
private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;
    Uri redirectionUri = new Uri(redirectionUrl);
    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials. 
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

I get a System.NullReferenceException: Object reference not set to an instance of an object. error in line: service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);

What am i missing?

EDIT (stack trace):

   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverRequest.GetResponseStream(IEwsHttpWebResponse response)
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverRequest.InternalExecute()
   at Microsoft.Exchange.WebServices.Autodiscover.GetUserSettingsRequest.Execute()
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetUserSettings(List`1 smtpAddresses, List`1 settings, Nullable`1 requestedVersion, Uri& autodiscoverUrl)
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetSettings[TGetSettingsResponseCollection,TSettingName](List`1 identities, List`1 settings, Nullable`1 requestedVersion, GetSettingsMethod`2 getSettingsMethod, Func`1 getDomainMethod)
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetUserSettings(List`1 smtpAddresses, List`1 settings)
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetSoapUserSettings(String smtpAddress, List`1 requestedSettings)
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetUserSettings(String userSmtpAddress, UserSettingName[] userSettingNames)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAutodiscoverUrl(String emailAddress, ExchangeVersion requestedServerVersion, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.AutodiscoverUrl(String emailAddress, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback)
   at Backend.Controllers.EposteController.PosljiMailTest() in C:\Users\\Desktop\Repos\\Controllers\EposteController.cs:line 216

EDIT 2: We managed to catch some errors on the Exchange server:

1**.***.***.*** GET /autodiscover/autodiscover.xml &CorrelationID=<empty>;&cafeReqId=***; 443 - 192.168.1.* - - 401 0 0 10
1**.***.***.*** POST /autodiscover/autodiscover.svc &CorrelationID=<empty>;&cafeReqId=***; 443 - 192.168.1.* ExchangeServicesClient/15.00.0913.015 - 401 0 0 19
1**.***.***.*** POST /autodiscover/autodiscover.svc &CorrelationID=<empty>;&cafeReqId=***; 443 - 192.168.1.* ExchangeServicesClient/15.00.0913.015 - 401 1 2148074254 12
1**.***.***.*** POST /autodiscover/autodiscover.svc &CorrelationID=<empty>;&cafeReqId=***; 443 domain\username 192.168.1.* ExchangeServicesClient/15.00.0913.015 - 200 0 0 137
like image 227
TheMixy Avatar asked Nov 14 '20 14:11

TheMixy


1 Answers

maybe I can provide an answer. I have an existing WPF application (.NET Framework 4.8, EntityFramework 6.4.4, Microsoft.Exchange.WebServices 2.2.0 (EWS) and other NuGet packages).

A week ago I started a migration to .NET 5, EntityFrameworkCore 5.0.7 and found that EWS was not .NET 5 compatible. There are NuGet packages under .NET Standard but autodiscover does not work according to the authors who advise to fill in Exchange EndPoints by hand.

I downloaded the source code from https://github.com/OfficeDev/ews-managed-api and made a .NET5 Class Library out of it. It compiles. At runtime, there are exceptions during autodiscover. I noticed that an Exchange response had a null ContentEncoding field while this property was used on the next line causing an Exception. The rest of the response seems to be correct, so I added a line at position 483 of AutoDiscoverRequest.cs as below:

if (contentEncoding == null) return responseStream;

It seems to work including autodiscover but this result is only a few hours old.

like image 152
Robertibus Avatar answered Nov 15 '22 10:11

Robertibus