【问题标题】:How to connect to Exchange?如何连接到 Exchange?
【发布时间】:2017-04-25 15:58:26
【问题描述】:

我想要在公司做的最简单的事情是检索邮件。我尝试了 Imap - 没有成功,(ImapX 根本没有连接,也没有显示错误)然后我来到了 EWS。

但也涉及一些巫术魔法。 这是有一些错误的代码:

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        service.UseDefaultCredentials = true;

        service.Url = new Uri("https://some.com/EWS/Exchange.asmx"); // The request failed. Unable to connect to the remote server
        var folder = Folder.Bind(service, WellKnownFolderName.Inbox);

         ///////////////////another try
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        service.UseDefaultCredentials = true;

        service.AutodiscoverUrl("someone@some.com"); // Discover server not found
        var folder = Folder.Bind(service, WellKnownFolderName.Inbox);

但是,我可以连接到 wsdl 版本:

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        service.UseDefaultCredentials = true;

        service.Url = new Uri("https://some.com:444/EWS/Services.wsdl");//Wow! It worked.
        var folder = Folder.Bind(service, WellKnownFolderName.Inbox);//duh, Method Not Allowed ........
        return null;

我到底如何连接到 EWS?我能够通过 Outlook 进行连接,并从我的域帐户的 Autodiscover.xml 文件中获取了所有这些地址。这个问题让我大吃一惊。

更新

这是 IMAP 服务器的示例:

var client = new ImapX.ImapClient("imap.some.com", 993, true);
client.Connect(); //just do nothing. nothing is connected, no errors.

【问题讨论】:

  • 鉴于 所有 电子邮件客户端都可以使用 POP3 和 IMAP4 连接到 Exchange,您应该说明您尝试了什么以及问题所在。 Outlook 不使用 Web 服务进行连接,它使用 IMAP4
  • 至于您的代码 - Web 服务由 WSDL定义。没有“WSDL 版本”。如果您使用 WSDL URL 来生成代理,您甚至不需要指定 URL,它作为默认地址存储在代理本身中
  • 最后 - 您的第一个和第二个 URL 使用相同的端口。 SSL 使用端口 443。第二个 URL 使用端口 444
  • 我通过some.com/EWS/Exchange.asmx 获取的这个 URL,它只是向我展示了 svcutil.exe 的示例
  • @Panagiotis Kanavos 对不起,您的回答不正确。 Outlook 在连接到 Microsoft Exchange 环境时使用每个默认 MAPI 或更高版本的一个 MAPI over HTTP。也不使用 IMAP 或 POP3,因此默认情况下可能会被禁用。有关更多信息,请参阅here。还请注意,MAC 的 Outlook 使用 EWS(无 pop3 或 MPAI),如 here

标签: c# exchange-server


【解决方案1】:

确保您为 EWS 网络服务配置了自动发现功能。使用 microsoft test 连接工具分析 Exchange 发现设置:

https://testconnectivity.microsoft.com/

【讨论】:

  • 有没有比将我的凭据传递给第三方更安全的方法来检查它?
  • 我同意。正如他们在免责声明中提到的那样,“我们强烈建议您创建一个测试帐户以使用此工具,并在完成连接测试后立即删除此帐户。”如果这对您可行,请尝试创建一个测试帐户。
【解决方案2】:
public static class ExchangeServerService
{
    // The following is a basic redirection validation callback method. It 
    // inspects the redirection URL and only allows the Service object to 
    // follow the redirection link if the URL is using HTTPS. 
    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;
    }

    public static ExchangeService ConnectToService()
    {
        try
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new NetworkCredential(@"email", "password");
            service.AutodiscoverUrl(@"email", RedirectionUrlValidationCallback);

            return service;
        }
        catch (Exception ex)
        {
            // log exception maybe
            return null;
        }
    }
}

像这样使用它:

var ESserver = ExchangeServerService.Connect();

【讨论】:

  • 我知道这是来自 MSDN 的精确副本,不,这不起作用。未找到发现服务器。
  • 所以这是你的问题而不是实际的实现。
  • 是的,这是我的问题。这就是问题出现的原因。
猜你喜欢
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2017-07-07
  • 1970-01-01
  • 2022-09-29
  • 2015-10-03
相关资源
最近更新 更多