gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:00:20 26 4
gpt4 key购买 nike

从我们的 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

最佳答案

也许我可以提供一个答案。我有一个现有的 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) 返回 responseStream;
它似乎可以工作,包括自动发现,但这个结果只有几个小时。

关于c# - ASP.NET CORE 和 EWS Managed API 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64835051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com