Read email using web sharing services

This is my scenario: I have to read the email from 2010 sp2 accounts. I have to use Exchange Web Services, POP3 and IMAP are blocked. I have to test my application in an environment where people can access their accounts through a web browser only on the intranet. I cannot debug the application directly on this intranet. I have this snippet for accessing 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 has blocked a potentially unsafe redirect to https://autodiscover.colpatria.com/autodiscover/autodiscover.xml . To allow autodiscover to follow redirection, use the AutodiscoverUrl overload (string, AutodiscoverRedirectionUrlValidationCallback)."

So, the RedirectionUrlValidationCallback method has been implemented, I'm not sure if the URL is correct. The fact is that I get this error:

"Could not find autodiscover service."

Is it possible that auto-detection is not configured correctly? I am not an exchange administrator, how can I find out if autodiscover works? I need arguments to tell Exchange administrators this feature. Thanks for any help.

+8
source share
6 answers

More recently, having encountered similar problems and working on their solution, I found a useful utility that was / very useful in troubleshooting: EWS Editor It may not solve your problems, but can be used to quickly iterate through various configuration combinations very quickly that, hopefully, would shed light on your problems.

I used this application when working with a client to establish Autodiscover and Service URL connections for testing and updating Exchange servers. It was convenient not only for me, but also for the IT staff of the client. They downloaded and used the utility to check and verify their settings.

From http://ewseditor.codeplex.com :

Project Description

EWSEditor has three goals:

  • Demonstrate the functionality and ease of managing managed Exchange Web Services APIs using source code.

  • Demonstrate SOAP traffic for Exchange Web Services used to complete actions initiated through the Explorer user interface.

  • Help non-developers debug and understand Exchange stores by exploring items, folders, and their properties in depth

+3
source

Somehow you need to write the result of redirectionUrl . You will get this error if your redirectionUrl does not match the specified URI (i.e., your autodiscover authentication callback returns FALSE ). Of course, the URI redirectionUrl is not what you think. If you use SSL, you need to handle the redirect validation callback.

Since you cannot debug the application, perhaps you can send an email to yourself, enter the shared database or file, or perhaps use the applicationโ€™s event log (throwing an application exception).

Note. . The first error shows that the Autodiscover URI is https://autodiscover.colpatria.com/autodiscover/autodiscover.xml . Should this replace the existing line https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml ?

Also see the related SO Autodiscovery SO publication and Checking URLs for potentially unsafe redirects to MSDN .

+2
source

This is an old post, which, as I thought, I will give in a complete sample solution for the reported error. Just replace service.AutodiscoverUrl (" someuser@somedomain.org "); from System.Uri (" https://mail.somedomain.org/ews/Exchange.asmx ");

Here is the complete code block

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); service.Credentials = new WebCredentials("someuser", "somepassword"); //service.AutodiscoverUrl(" someuser@somedomain.org "); service.Url = new System.Uri("https://mail.somedomain.org/ews/Exchange.asmx"); 
+1
source

Try service.TraceEnabled = true;

WFM. In my case, I needed to install SSL / TLS by installing the certificate from the Exchange server to the client computer. I got this solution from trace output.

0
source

This works like a charm to me:

  var exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1); var username = Settings.EmailUserName; var password = Settings.EmailPassword; var domain = Settings.EmailDomain; var email = Settings.Email; exchange.Credentials = new WebCredentials(email, password); exchange.AutodiscoverUrl(email, RedirectionCallback); 

and RedirectionCallback:

  static bool RedirectionCallback(string url) { // Return true if the URL is an HTTPS URL. return url.ToLower().StartsWith("https://"); } 

heres is a link: https://msdn.microsoft.com/en-us/library/office/dd635285(v=exchg.80).aspx

Hello!

0
source

When you used AutodiscoverUrl () with RedirectionUrlValidationCallback, here is a sample code:

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.PreAuthenticate = true; service.Credentials = new WebCredentials("my_username","my_password"); //use WebCredentials instead of NetworkCredential service.AutodiscoverUrl(userEmailAddress, RedirectionCallback); 

And the RedirectionCallback method should be like this:

  static bool RedirectionCallback(string url) { bool redirectionValidated = false; Uri redirectionUri = new Uri(url); //There are two ways of implementing a RedirectionCallback scheme // Way 1: Return true if the URL is an HTTPS URL. //return url.ToLower().StartsWith("https://"); if (redirectionUri.Scheme == "https") redirectionValidated = true; //Way 2: check if url is autodiscovery url if (url.Equals( "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml")) redirectionValidated = true; return redirectionValidated; } 

PS: Take care of proxies that prohibit the Autodiscover service. In my case, this code returned the error "Autodiscover service cannot be found" every time, but the main reason was 403 Forbidden when calling autodiscover. This works after setting up the proxy.

0
source

All Articles