read email using exchange web services

user994432 picture user994432 · Jul 13, 2012 · Viewed 24.6k times · Source

This is my scenario: I have to read email from exchange 2010 sp2 accounts. I have to use Exchange Web Services, POP3 and IMAP are blocked. I have to test my app in an environment where people can access their accounts through a web browser only in the intranet. I can't debug my app directly to this intranet. I have this snippet to access an account:

private void Dowork()
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

    string dominio = "domain";
    string usuario = "user";
    string password = "password";

    service.Credentials = new NetworkCredential(usuario, password, dominio);

    string url = usuario + "@" + dominio + ".com";

    service.AutodiscoverUrl(url, RedirectionUrlValidationCallback);
    //service.AutodiscoverUrl(url);

    FindItemsResults<Item> findResults = service.FindItems(
       WellKnownFolderName.Inbox,
       new ItemView(10));

    string content = string.Empty;

    foreach (Item item in findResults.Items)
    {
        EmailMessage email = EmailMessage.Bind(service, item.Id);
        email.Load();

        content += item.Subject + "\n";
        content += email.From.Address + "\n";
        content += email.Body + "\n\n";

        //Console.WriteLine(item.Subject);
        //Console.WriteLine(email.From.Address);
        //Console.WriteLine(email.Body);
    }

    string result = content;
}

// Create the callback to validate the redirection URL.
static bool RedirectionUrlValidationCallback(String redirectionUrl)
{
    // Perform validation.
    return (redirectionUrl == "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml");
}

If I use this line:

service.AutodiscoverUrl(url);

I get this error:

"Autodiscover blocked a potentially insecure redirection to https://autodiscover.colpatria.com/autodiscover/autodiscover.xml. To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) overload."

So the method RedirectionUrlValidationCallback was implemented, I'm not sure if the url is right. The fact is I'm getting this error:

"The Autodiscover service couldn't be located".

Is possible that Autodiscover is not configured properly?? I'm not the exchange administrator, how can I know if autodiscover works?? I need arguments to tell exchange administrators this feature must be configured. Thanks for any help.

Answer

GoBeavs picture GoBeavs · Feb 1, 2016

This is a old post I thought I'd put in a full example solution for the error reported. Simply replace service.AutodiscoverUrl("[email protected]"); with System.Uri("https://mail.somedomain.org/ews/Exchange.asmx");

Here's the full block of code

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                service.Credentials = new WebCredentials("someuser", "somepassword");
                //service.AutodiscoverUrl("[email protected]");
                service.Url = new System.Uri("https://mail.somedomain.org/ews/Exchange.asmx");