问题
Sending a simple email through ews is working as intended - from my account to my account:
ExchangeService ews = new ExchangeService(ExchangeVersion.Exchange2010);
ews.AutodiscoverUrl("myname@mydomain.com");
EmailMessage email = new EmailMessage(ews);
email.ToRecipients.Add("myname@mydomain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
email.Send();
Simply trying impersonation, it is also working as intended - in the last line, it returns the error that I am not allowed to impersonate:
ExchangeService ews = new ExchangeService(ExchangeVersion.Exchange2010);
ews.AutodiscoverUrl("myname@mydomain.com");
ews.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "testuser@mydomain.com");
EmailMessage email = new EmailMessage(ews);
email.ToRecipients.Add("myname@mydomain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
email.Send();
Now I try to login with my application service account instead:
ExchangeService ews = new ExchangeService(ExchangeVersion.Exchange2010);
ews.Credentials = new NetworkCredential("service", "1234", "mydomain.com");
//ews.Credentials = new WebCredentials("service", "1234");
ews.AutodiscoverUrl("myname@mydomain.com");
//ews.AutodiscoverUrl("service@mydomain.com");
ews.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "testuser@mydomain.com");
EmailMessage email = new EmailMessage(ews);
email.ToRecipients.Add("myname@mydomain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
email.Send();
But here it throws an error in the autodiscover line: "AutodiscoverLocalException: The Autodiscover service couldn't be located."
The service account is set up in AD and Exchange, with correct password and smtp address.
Why isn't it working? How can I check what's causing that error?
回答1:
I solved the problem, and guess what the problem is: it's the user account.
EWS uses the given credentials to authenticate itself for access to the Autodiscover service at
http://mydomain/AutoDiscover/AutoDiscover.xml
The credentials were correct, but it seems that for accounts set to "user has to change password on first login", access to the autodiscover service is denied. I changed that setting in AD and now it works.
回答2:
If this is a local domain, try using mydomain
rather than mydomain.com
I found that .com
or .local
didn't work for me.
If it is an Office365 server, use null
or string.Empty
for the domain.
回答3:
i added following lines in host file & it worked for me;
192.168.32.43 mail.mydomain.com
192.168.32.43 autodiscover.mydomain.com
来源:https://stackoverflow.com/questions/19909442/exchange-impersonation-in-service-account-autodiscover-service-couldnt-be-loca