首页 > 解决方案 > ASP.NET CORE 和 EWS 托管 API 连接问题

问题描述

从我们的 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();
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;
}

我收到一条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

编辑 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

标签: c#asp.net-coreexchangewebservices

解决方案


也许我可以提供一个答案。我有一个现有的 WPF 应用程序(.NET Framework 4.8、EntityFramework 6.4.4、Microsoft.Exchange.WebServices 2.2.0 (EWS) 和其他 NuGet 包)。

一周前,我开始迁移到 .NET 5,EntityFrameworkCore 5.0.7,发现 EWS 与 .NET 5 不兼容。.NET Standard 下有 NuGet 包,但根据建议手动填写 Exchange EndPoints 的作者的说法,自动发现不起作用。

我从https://github.com/OfficeDev/ews-managed-api下载了源代码,并用它制作了一个 .NET5 类库。它编译。在运行时,自动发现期间会出现异常。我注意到一个 Exchange 响应有一个空的 ContentEncoding 字段,而在下一行使用此属性会导致异常。其余的响应似乎是正确的,所以我在 AutoDiscoverRequest.cs 的 483 位置添加了一行,如下所示:

if (contentEncoding == null) 返回响应流;

它似乎可以工作,包括自动发现,但这个结果只有几个小时。


推荐阅读