首页 > 解决方案 > EWS 减少邮箱连接时间

问题描述

我有一个用于读取传入邮件 Outlook(Exchange Server 2016)的程序,该程序使用帐户“system”在任务调度程序窗口中运行,但在连接步骤中,程序等待几分钟。如果程序开始使用域帐户,则连接是在没有预期的情况下进行的。我不想更改任务计划程序窗口中的设置。我认为问题出在连接超时,如何减少连接时间并在域授权后开始搜索邮箱?

我在连接时使用它:

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);

标签: c#authenticationexchange-serverexchangewebservices

解决方案


我会从

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

EWS 代码不需要它,我看不到您使用它返回的对象。您可以使用秒表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);

推荐阅读