ASP.NET CORE 和 EWS Managed API 连接问题

The*_*ixy 6 c# exchangewebservices asp.net-core

从我们的 Intranet Web 应用程序(Asp.net core 3)中,我想为用户添加发送电子邮件的功能。我们有一个本地 MS Exchange Server 2016(我们域的一部分)。

按照微软的这个例子:https : //docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications 我已经把将以下代码一起发送测试电子邮件:

//exchange
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.UseDefaultCredentials = true;
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl("name.surname@mydomain.com", RedirectionUrlValidationCallback);
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("recepient@mydomain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
email.Send();
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)

我得到一个System.NullReferenceException: Object reference not set to an instance of an object. 错误:service.AutodiscoverUrl("name.surname@mydomain.com", RedirectionUrlValidationCallback);

我错过了什么?

编辑(堆栈跟踪):

   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
Run Code Online (Sandbox Code Playgroud)

编辑 2:我们设法在 Exchange 服务器上捕获了一些错误:

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
Run Code Online (Sandbox Code Playgroud)

cfi*_*zer 0

您是否缺少redirectionUrl 参数?

service.AutodiscoverUrl("name.surname@mydomain.com", (redirectUrl) => { return RedirectionUrlValidationCallback(redirectUrl);});
Run Code Online (Sandbox Code Playgroud)