![](/img/trans.png)
[英]EWS 401 Unauthorized - But only when running under a service on the target server
[英]401 UnAuthorized Access when using EWS to connect to mailbox
這是我要解決的方案:我的用戶“ myuser”可以訪問服務郵箱“ serviceMail”。 因此,這不是我的個人郵箱,而是我公司發送的用於各種目的的電子郵件的另一個郵箱設置。 我知道我可以使用它,因為我已經在Outlook中添加了該郵箱,並且能夠像平常一樣查看收件箱中自己的電子郵件。 我正在嘗試使用EWS編寫ac#程序,該程序將從“ serviceMail”收件箱中的電子郵件中提取附件。 嘗試查找項目時出現“ 401未經授權的訪問”。 我究竟做錯了什么? 我的代碼如下。
這是我連接服務的方式:
public ExchangeService ConnectToExchangeServer()
{
const string strMailbox = "serviceMail@abc.com";
const string strLoginUser = "mysuer@abc.com";
const string strLogingUserpwd = "pwd";
const string strO365Url = "https://outlook.office365.com/EWS/Exchange.asmx";
try
{
exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
exchange.Credentials = new WebCredentials(strLoginUser, strLogingUserpwd, "sabra.com");
// exchange.AutodiscoverUrl(strMailbox,RedirectionUrlValidationCallback);
exchange.Url = new Uri(strO365Url);
return exchange;
}
catch (Exception ex)
{
}
return exchange;
}
下面是嘗試找到項目的代碼
ExchangeService service = ga.ConnectToExchangeServer();
TimeSpan ts = new TimeSpan(0, -1, 0, 0);
DateTime date = DateTime.Now.Add(ts);
SearchFilter.IsGreaterThanOrEqualTo filter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
if (service != null)
{
//FindItemsResults<Item> findResults = ga.exchange.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50));
//FindItemsResults<Item> findResults = ga.exchange.FindItems(WellKnownFolderName.Inbox,);
// Return a single item.
ItemView view = new ItemView(1);
string querystring = "HasAttachments:true Subject:'Message with Attachments' Kind:email";
// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, querystring, view);
foreach (Item item in findResults)
{
EmailMessage message = EmailMessage.Bind(ga.exchange, item.Id);
if (message.HasAttachments && message.Attachments[0] is FileAttachment)
{
FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
//Change the below Path
fileAttachment.Load(@"D:\\QlikData\\Lean\\EmailExtract" + fileAttachment.Name);
// lblAttach.Text = "Attachment Downloaded : " + fileAttachment.Name;
}
else
{
// MessageBox.Show("No Attachments found!!");
}
}
if (findResults.Items.Count <= 0)
{
//lstMsg.Items.Add("No Messages found!!");
}
}
我收到錯誤消息“請求失敗。遠程服務器返回錯誤:(401)未經授權。” 在FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox,querystring,view)行上。
有任何想法嗎?
您的代碼將僅訪問呼叫帳戶郵箱的收件箱。 您需要使用FolderId重載來指定要訪問的實際郵箱。 請參閱https://msdn.microsoft.com/zh-cn/library/office/dn641957(v=exchg.150).aspx中的 “顯式訪問和EWS托管API”
您還錯誤地在憑據中指定了用戶名,例如,您應該使用下層格式netbiosdomain \\ username或使用UPN https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa380525(v=vs .85).aspx,並在這種情況下忽略域。 參見https://social.technet.microsoft.com/Forums/zh-CN/12de5368-dde0-4d91-a1b2-394c4487d0f1/ews-the-request-failed-the-remote-server-returned-an-error-401 -unauthorized?論壇= exchangesvrdevelopment
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.