2017-04-11 190 views
0

這是我嘗試解決的情況:我可以使用用戶myuser訪問服務郵箱serviceMail。所以這不是我的個人郵箱,而是我公司的另一個郵箱設置,我們收到了用於各種目的的電子郵件。我知道我有權訪問它,因爲我在Outlook中添加了這個郵箱,並且能夠像通常一樣爲我自己的電子郵件檢查收件箱。我正嘗試使用EWS編寫一個c#程序,該程序將從'serviceMail'收件箱中的電子郵件中獲取附件。嘗試查找項目時,我正在獲得「401 Unauthroized Access」。我究竟做錯了什麼?我的代碼如下。401使用EWS連接郵箱時未經授權訪問

這裏是我如何連接到服務:

public ExchangeService ConnectToExchangeServer() 
    { 



     const string strMailbox = "[email protected]"; 
     const string strLoginUser = "[email protected]"; 
     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)代碼行。

任何想法?

回答

0

您的代碼只能訪問主叫帳戶郵箱的收件箱。您需要使用FolderId重載來指定您想要訪問的實際郵箱。看到https://msdn.microsoft.com/en-us/library/office/dn641957(v=exchg.150).aspx

「明確的訪問與EWS託管API」你也specifing的用戶名的憑據不正確如您需使用和下級格式netbiosdomain \用戶名或使用UPN https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspx,而忽略在這種情況下,域。見https://social.technet.microsoft.com/Forums/en-US/12de5368-dde0-4d91-a1b2-394c4487d0f1/ews-the-request-failed-the-remote-server-returned-an-error-401-unauthorized?forum=exchangesvrdevelopment