低于错误的跟踪次数
Microsoft.Exchange.WebServices.dll中发生了类型为'Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException'的未处理异常
ExchangeService oews = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
{
Credentials = new WebCredentials("mohanb@cubicsp.onmicrosoft.com","******") //state your Exchange username,Exchange Password and Exchange Domain
};
oews.AutodiscoverUrl("mohanb@cubicsp.onmicrosoft.com"); //User Mailbox whose inbox is to be accessed.
FindFoldersResults foundFolderResults = oews.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue));
MEWS.Folder exchangeExchangeAPIArchivedFolder = foundFolderResults.Folders.ToList().Find(
f => f.DisplayName.Equals("SentItem", StringComparison.CurrentCultureIgnoreCase));
我在这里使用 https://outlook.office365.com/EWS/Exchange.asmx 对代码进行了更改 现在我收到此错误
Microsoft.Exchange.WebServices.dll中发生了类型为'Microsoft.Exchange.WebServices.Data.ServiceRequestException'的未处理异常
其他信息:请求失败。远程服务器返回错误:(401)未经授权。
ExchangeService _service = new ExchangeService();
_service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(10))) {
email.Load(new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.TextBody));
string recipients = "";
foreach (EmailAddress emailAddress in email.CcRecipients)
{
recipients += ";" + emailAddress.Address.ToString();
}
string internetMessageId = email.InternetMessageId;
string fromAddress = email.From.Address;
string recipient = recipients;
string subject = email.Subject;
}
预先感谢
答案 0 :(得分:0)
401表示您的凭据错误。
答案 1 :(得分:0)
这可能是身份验证问题。
您可以添加以下代码:
service.PreAuthenticate = true;
service.Credentials = new WebCredentials("YouEmailAdress","Password");
这是完整的代码:
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
//ICredentials creds = new NetworkCredential("xxxx", "xxxx.com");
service.Credentials = new WebCredentials("YouEmailAdress","Password");
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
service.PreAuthenticate = true;
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, " YouEmailAdress");
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, SetFilter(), view);
foreach (Item item in findResults.Items)
{
if (item.Subject != null)
{
list.Add(item.Subject.ToString());
}
else
{
list.Add("test");
}
list.Add(item.DateTimeSent.ToString());
}
}
private static SearchFilter SetFilter()
{
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));
SearchFilter s = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
return s;
}
private static bool CertificateValidationCallBack(
object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
// If the certificate is a valid, signed certificate, return true.
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
// If there are errors in the certificate chain, look at each error to determine the cause.
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{
if (chain != null && chain.ChainStatus != null)
{
foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
{
if ((certificate.Subject == certificate.Issuer) &&
(status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
{
// Self-signed certificates with an untrusted root are valid.
continue;
}
else
{
if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
{
// If there are any other errors in the certificate chain, the certificate is invalid,
// so the method returns false.
return false;
}
}
}
}
// When processing reaches this line, the only errors in the certificate chain are
// untrusted root errors for self-signed certificates. These certificates are valid
// for default Exchange server installations, so return true.
return true;
}
else
{
// In all other cases, return false.
return false;
}
}
如果您使用的帐户不是您的帐户,则需要确保根据下面提到的文章,我们具有EWS模拟的必需权限: