1

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.
eocron
  • 6,885
  • 1
  • 21
  • 50
  • Given that *all* email clients can connect to Exchange using POP3 and IMAP4, you should explain what you tried and what was the problem. Outlook doesn't use web services to connect, it uses IMAP4 – Panagiotis Kanavos Apr 25 '17 at 16:05
  • As for your code - web services are *defined* by WSDL. There is no "WSDL version". If you use the WSDL URL to generate your proxy, you don't even need to specify the URL, it's stored as a default address in the proxy itself – Panagiotis Kanavos Apr 25 '17 at 16:08
  • Finally - your first and second URLs are *not* using the same port. SSL uses port 443. The second URL uses port 444 – Panagiotis Kanavos Apr 25 '17 at 16:09
  • This URL I aquired through https://some.com/EWS/Exchange.asmx, it just shows me example with svcutil.exe – eocron Apr 25 '17 at 16:13
  • @Panagiotis Kanavos I´m sorry, but your answer is not correct. Outlook when connected to an Microsoft Exchange environment use per default MAPI or later one MAPI over HTTP. Nor IMAP or POP3 is used and might be therefore disabled per default. For more infos see [here](https://technet.microsoft.com/en-us/library/dn635177(v=exchg.160).aspx). Keep also noted that Outlook for MAC use EWS (no pop3 or MPAI) as written [here](https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_mac/outlook-2016-for-mac-does-it-support-ews-exchange/a5fa8065-d90f-4df3-b35e-7c277006aad1) – BastianW Apr 25 '17 at 18:17
  • @eocron are you using MS Exchange 2007? it looks like. If this is the case please keep noted that [Exchange 2007 is end of support since 11 April 2017](http://www.admin-enclave.com/en/articles/exchange/366-microsoft-exchange-2007-reached-end-of-life-today.html). – BastianW Apr 25 '17 at 18:27
  • Probably, 2013, but don't think this is the problem. I tried out all Enum values in cycle with no success in either of them. – eocron Apr 25 '17 at 20:53

2 Answers2

2

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/

vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • Is there more secure way to check it rather than passing my credentials to third party? – eocron Apr 25 '17 at 16:08
  • I agree.. As they mentioned in disclaimer "we strongly recommend that you create a test account for the purpose of using this tool, and delete this account immediately after you've completed the connectivity testing." Try creating a test account if that's feasible for you. – vendettamit Apr 25 '17 at 17:01
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;
        }
    }
}

Use it like:

var ESserver = ExchangeServerService.Connect();
Cristian Szpisjak
  • 2,429
  • 2
  • 19
  • 32