问题
This Microsoft page indicates that by setting the UseDefaultCredentials property to true, no login name and password are required to communicate with the Exchange server. However, that is not my experience.
My code creates an instance of ExchangeService called service:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
If I manually set the credentials as follows, everything works just fine:
service.Credentials = new WebCredentials("my@email.address", "my password");
However, if I remove that line and replace it with the following, my code doesn't work:
service.UseDefaultCredentials = true;
I have searched and searched for solutions but I'm not finding anything concrete. If someone here could please help me I would be very grateful.
EDIT: I tried to use my own credentials instead of the Default Credentials.
string smtpaddress = "somesharedsmtp@domain.com";
es.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
es.Credentials = new WebCredentials("myemail", "mypassword");
es.AutodiscoverUrl(smtpaddress, RedirectionCallback);
FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox, "somesharedsmtp@domain.com");
Folder sharedInbox = Folder.Bind(es, SharedMailbox);
At this point I can check sharedInbox containing the details of the Inbox. I can see the unread email count and others.
But when I access
ItemView itemView = new ItemView(100);
FindItemsResults<Item> findResults1 = es.FindItems(pqInbox.Id, itemView);
I get exception "SMTP address has no mailbox associated with it"
回答1:
my experience in that case is, that if you set service.UseDefaultCredentials = true your Login of your pc will be taken. Which works perfect if your in the same ActiveDirectory structure as the exchangeserver is. If your trying to connect to the exchange server from outside of the ADS you will have to set the credentials manually.
I'm using the defaultcredentials for my tools when I'm at work. If I connect from at home I have to set the credentials.
Hope that helps you...
回答2:
From MSDN documentation, UseDefaultCredentials is ignored in Exchange Online. You must specify credentials for Exchange Online.
http://msdn.microsoft.com/en-us/library/office/microsoft.exchange.webservices.data.exchangeservicebase.usedefaultcredentials(v=exchg.80).aspx
来源:https://stackoverflow.com/questions/15481717/exchange-web-services-usedefaultcredentials-property