EWS を使用して受信メールにアクセスしようとしています。Kerberos が構成されました。しかし、webpart で service.UseDefaultCredentials = true を使用すると、401 エラー (未承認) が発生します。この問題を解決するための解決策は何ですか。
private static bool ValidateCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors) {
return true;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);
service.UseDefaultCredentials = true;
service.Url = new Uri(EWSuri.uri);
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
new SearchFilter.IsEqualTo(
EmailMessageSchema.IsRead, false), new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, DateTime.Now.AddDays(-14)));
FindItemsResults<Item> findResults = service.FindItems(
WellKnownFolderName.Inbox, sf,
new ItemView(int.MaxValue));
emailblock.InnerHtml = "<div data-itemscount=\"" + findResults.Items.Count + "\"></div>";
foreach (Item item in findResults.Items)
{
EmailMessage mes = (EmailMessage)item;
emailblock.InnerHtml += "<div class=\"emailitem\"><p class=\"emailline clearfix\"><span class=\"emailfield\">From:</span>" +
"<span class=\"emailtext\">" + " " + mes.Sender.Name + "</span></p>" +
"<p class=\"emailline clearfix\"><span class=\"emailfield\">Subject:</span>" +
"<span class=\"emailtext\">" + " " + item.Subject + "</span></p>" +
"<p class=\"emailline clearfix\"><span class=\"emailfield\">Received:</span>" +
"<span class=\"emailtext\">" + " " + item.DateTimeSent + "</span></p></div>";
}
}
catch (Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverRemoteException ex)
{
Console.WriteLine("Exception thrown:{0}" ,ex.Message);
}
}