EWS 减少邮箱连接时间

EWS reduce mailbox connection time

我有一个程序可以读取收到的邮件 outlook (exchange server 2016) 该程序使用帐户 "system" 在任务计划程序 windows 中运行,但在连接步骤中程序会等待几分钟。 如果程序开始使用域帐户,则连接会意外建立。 我不想更改任务计划程序中的设置 windows。 我认为问题出在连接超时,如何减少连接时间并在域授权后开始搜索邮箱?

我在连接时使用这个:

string url = "Url";
string UserLogin = "UserLogin";
string UserPassword = "UserPassword";
string UserDomain = "UserDomain";
string UserEmail = "UserEmail";

ExchangeService ews = new ExchangeService();
UserPrincipal userP = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, UserDomain, UserLogin, UserPassword), Environment.UserName);
ews.Credentials = new WebCredentials(UserLogin, UserPassword, UserDomain);
Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverRedirectionUrlValidationCallback RedirectionUrlValidationCallback = null;
ews.AutodiscoverUrl(UserEmail, RedirectionUrlValidationCallback);

Folder inbox = Folder.Bind(ews, WellKnownFolderName.Inbox);
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Body);

我会从

开始
UserPrincipal userP = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, UserDomain, UserLogin, UserPassword), Environment.UserName);

EWS 代码不需要它,我看不到你在使用它 returns。您可以使用 StopWatch https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=netframework-4.8 并为每一行代码计时,并将其写入日志文件以查看延迟的位置。

您可以做的其他测试(如果它有所作为)是对 EWS 端点进行硬编码并删除自动发现操作(默认情况下,这会执行多个 DNS 和 LDAP 查询来计算出端点,所以请尝试)

        string url = "Url";
        string UserLogin = "UserLogin";
        string UserPassword = "UserPassword";
        string UserDomain = "UserDomain";
        string UserEmail = "UserEmail";
        string EwsEndpoint = "https://yourserver.youneedtodefine.com/EWS/Exchange.asmx";

        ExchangeService ews = new ExchangeService();
        ews.Credentials = new WebCredentials(UserLogin, UserPassword, UserDomain);
        ews.Url = new Uri(EwsEndpoint);

        Folder inbox = Folder.Bind(ews, WellKnownFolderName.Inbox);
        PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Body);