Ani Evgenieva
Ani Evgenieva

Reputation: 11

C# EWS in Exchange AutodiscoverUrl Exception while using Version 2010 or latest

a had successful connection with ExchangeVersion.Exchange2007_SP1, when I changed it on 2010 or latest I get the exception on this row:

service.AutodiscoverUrl(MailboxToAccess, RedirectionUrlValidationCallback); ->System.NullReferenceException: 'Object reference not set to an instance of an object.' I tried with Packages: Exchange.WebServices.Managed.Api v2.2.1.2 and Microsoft.Exchange.WebServices v2.2.0

static void Main(string[] args)
        {
            String MailboxToAccess = "username@domain";
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
            SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);

            service.Credentials = new WebCredential("username@domain", "password");
            service.AutodiscoverUrl(MailboxToAccess, RedirectionUrlValidationCallback);

            SetStreamingNotifications(service);
        }
 private static bool RedirectionUrlValidationCallback(string redirectionUrl)
        {
            bool result = false;

            Uri redirectionUri = new Uri(redirectionUrl);
            if (redirectionUri.Scheme == "https")
            {
                result = true;
            }
            return result;
        }

Upvotes: 0

Views: 1013

Answers (1)

Ani Evgenieva
Ani Evgenieva

Reputation: 11

I set the Exchange service URL manually using

/ Create the binding.
ExchangeService service = new ExchangeService();
// Set the credentials for the on-premises server.
service.Credentials = new WebCredentials("[email protected]", "password");
// Set the URL.
service.Url = new Uri("https://computername.domain.contoso.com/EWS/Exchange.asmx");

I still don't understand why It didn't work for me with

service.AutodiscoverUrl("[email protected]");

Upvotes: 1

Related Questions