"Autodiscover Service could not be located" when using EWS basic authentication

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3134745%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%22Autodiscover%20Service%20could%20not%20be%20located%22%20when%20using%20EWS%20basic%20authentication%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3134745%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%20class%3D%22%5C%26quot%3B%5C%26quot%3B%22%3EHello%2C%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CP%3EI%20want%20to%20use%20EWS%20managed%20API%20with%20basic%20authentication%20to%20connect%20to%20Exchange%20Online.%20I%20am%20using%20the%20following%20sample%20code%20to%20monitor%20my%20mailbox%3A%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2FDIV%26gt%3B%26lt%3B%5C%2FDIV%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eusing%26nbsp%3BSystem%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eusing%26nbsp%3BSystem.Collections.Generic%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eusing%26nbsp%3BSystem.Linq%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eusing%26nbsp%3BSystem.Text%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eusing%26nbsp%3BSystem.Threading.Tasks%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eusing%26nbsp%3BMicrosoft.Exchange.WebServices.Data%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Enamespace%26nbsp%3BEWS_Read%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3Bclass%26nbsp%3BProgram%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bstatic%26nbsp%3Bvoid%26nbsp%3BMain(string%5B%5D%26nbsp%3Bargs)%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Btry%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BExchangeService%26nbsp%3Bexchange%26nbsp%3B%3D%26nbsp%3Bnew%26nbsp%3BExchangeService()%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bexchange.Credentials%26nbsp%3B%3D%26nbsp%3Bnew%26nbsp%3BWebCredentials(%22Email%20address%20removed%22%2C%26nbsp%3B%22password%22)%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bexchange.AutodiscoverUrl(%22Email%20address%20removed%22%2C%26nbsp%3BRedirectionUrlValidationCallback)%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bif%26nbsp%3B(exchange%26nbsp%3B!%3D%26nbsp%3Bnull)%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BFindItemsResults%3CITEM%3E%26nbsp%3Bresult%26nbsp%3B%3D%26nbsp%3Bexchange.FindItems(WellKnownFolderName.Inbox%2C%26nbsp%3Bnew%26nbsp%3BItemView(100))%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FITEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bforeach%26nbsp%3B(Item%26nbsp%3Bitem%26nbsp%3Bin%26nbsp%3Bresult)%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BEmailMessage%26nbsp%3Bmessage%26nbsp%3B%3D%26nbsp%3BEmailMessage.Bind(exchange%2C%26nbsp%3Bitem.Id)%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bstring%26nbsp%3Bbody%26nbsp%3B%3D%26nbsp%3Bmessage.Body.Text%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BString%26nbsp%3Bfrom%26nbsp%3B%3D%26nbsp%3Bmessage.From.Id.ToString()%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BString%26nbsp%3Bto%26nbsp%3B%3D%26nbsp%3Bmessage.ToRecipients.ToString()%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BString%26nbsp%3Bcc%26nbsp%3B%3D%26nbsp%3Bmessage.CcRecipients.ToString()%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BString%26nbsp%3Bsubject%26nbsp%3B%3D%26nbsp%3Bmessage.Subject.ToString()%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bcatch%26nbsp%3B(Exception%26nbsp%3Bex)%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BConsole.WriteLine(ex.Message)%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bprivate%26nbsp%3Bstatic%26nbsp%3Bbool%26nbsp%3BRedirectionUrlValidationCallback(string%26nbsp%3BredirectionUrl)%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bbool%26nbsp%3Bresult%26nbsp%3B%3D%26nbsp%3Bfalse%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BUri%26nbsp%3BredirectionUri%26nbsp%3B%3D%26nbsp%3Bnew%26nbsp%3BUri(redirectionUrl)%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bif%26nbsp%3B(redirectionUri.Scheme%26nbsp%3B%3D%3D%26nbsp%3B%22https%22)%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bresult%26nbsp%3B%3D%26nbsp%3Btrue%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Breturn%26nbsp%3Bresult%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EI%20have%20tried%20by%20using%20Microsoft%20connect%20analyzer%20(%3CA%20href%3D%22%5C%26quot%3Bhttps%3A%2F%2Ftestconnectivity.microsoft.com%2Ftests%2FO365ExchangeDns%2Finput%5C%26quot%3B%22%20target%3D%22%5C%26quot%3B_blank%5C%26quot%3B%22%20rel%3D%22%5C%26quot%3Bnoopener%20nofollow%20noopener%20noreferrer%22%20noreferrer%3D%22%22%3Ehttps%3A%2F%2Ftestconnectivity.microsoft.com%2Ftests%2FO365ExchangeDns%2Finput%26lt%3B%5C%2FA%26gt%3B)%20and%20could%20see%20that%20the%20Autodiscover%20service%20is%20active.%20The%20above%20sample%20code%20gives%20an%20error%20saying%20%22Autodiscover%20Service%20could%20not%20be%20located%22.%20Can%20you%20please%20help%20me%20out%20with%20your%20suggestions.%26lt%3B%5C%2FP%26gt%3B%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EThanks%2C%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EAnkit%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3CLINGO-LABS%20id%3D%22%5C%26quot%3Blingo-labs-3134745%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CLINGO-LABEL%3EExchange%20Online%26lt%3B%5C%2Flingo-label%26gt%3B%3CLINGO-LABEL%3EExchange%20Server%26lt%3B%5C%2Flingo-label%26gt%3B%3CLINGO-LABEL%3EHybrid%26lt%3B%5C%2Flingo-label%26gt%3B%3CLINGO-LABEL%3EOffice%20365%26lt%3B%5C%2Flingo-label%26gt%3B%3CLINGO-LABEL%3EOutlook%26lt%3B%5C%2Flingo-label%26gt%3B%26lt%3B%5C%2Flingo-labs%26gt%3B%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3C%2FLINGO-SUB%3E
Occasional Visitor

Hello,

 

I want to use EWS managed API with basic authentication to connect to Exchange Online. I am using the following sample code to monitor my mailbox:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Exchange.WebServices.Data;

namespace EWS_Read

{

   class Program

   {

      static void Main(string[] args)

      {

         try

         {

            ExchangeService exchange = new ExchangeService();

            exchange.Credentials = new WebCredentials("Email address removed", "password");

            exchange.AutodiscoverUrl("Email address removed", RedirectionUrlValidationCallback);

            if (exchange != null)

            {

               FindItemsResults<Item> result = exchange.FindItems(WellKnownFolderName.Inbox, new ItemView(100));

               foreach (Item item in result)

               {

                  EmailMessage message = EmailMessage.Bind(exchange, item.Id);

                  string body = message.Body.Text;

                  String from = message.From.Id.ToString();

                  String to = message.ToRecipients.ToString();

                  String cc = message.CcRecipients.ToString();

                  String subject = message.Subject.ToString();

               }

            }

         }

         catch (Exception ex)

         {

            Console.WriteLine(ex.Message);

         }

      }

      private static bool RedirectionUrlValidationCallback(string redirectionUrl)

      {

         bool result = false;

         Uri redirectionUri = new Uri(redirectionUrl);

         if (redirectionUri.Scheme == "https")

         {

            result = true;

         }

         return result;

      }

   }

}

 

I have tried by using Microsoft connect analyzer (https://testconnectivity.microsoft.com/tests/O365ExchangeDns/input) and could see that the Autodiscover service is active. The above sample code gives an error saying "Autodiscover Service could not be located". Can you please help me out with your suggestions.

 

Thanks,

Ankit

0 Replies