How to connect to Exchange?

邮差的信 提交于 2020-01-06 17:59:27

问题


The simplest thing I want in my company - is to retrieve mails. I tried over Imap - no success, (ImapX not connecting at all and no error is shown) and I came to EWS.

But there is also some voo-doo magic involved. And here is the code with some errors:

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

However, I'm able to connect to wsdl version:

        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;

How the heck do I connect to EWS? I'm able to connect through Outlook, and aquired all this addresses from its Autodiscover.xml file of my domain account. This question blowing my head.

UPDATE

Here is example with IMAP server:

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

回答1:


Make sure you have autodisocver configure for EWS webservices. Use the microsoft test connectivity tool to analyze the exchange discovery settings:

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

Use it like:

var ESserver = ExchangeServerService.Connect();


来源:https://stackoverflow.com/questions/43615632/how-to-connect-to-exchange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!