Forum Discussion
SahaAnki
Feb 08, 2022Copper Contributor
"Autodiscover Service could not be located" when using EWS basic authentication
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
No RepliesBe the first to reply