如何连接到Exchange?

时间:2017-04-25 15:58:26

标签: c# exchange-server

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

但也有一些voo-doo魔法涉及。 以下是包含一些错误的代码:

        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.

2 个答案:

答案 0 :(得分:1)

确保您为EWS Web服务配置了autodisocver。使用microsoft测试连接工具分析交换发现设置:

https://testconnectivity.microsoft.com/

答案 1 :(得分:0)

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();
相关问题