2017-04-11 108 views
0

私は解決しようとしています。私は自分のユーザー 'myuser'をサービスメールボックス 'serviceMail'にアクセスできます。だから、これは私の個人的なメールボックスではなく、さまざまな目的のために電子メールを送信した私の会社の別のメールボックス設定です。私はOutlookにこのメールボックスを追加したので、私はそれにアクセスできることを知っています。私は自分のメールのために普通のように受信トレイをチェックすることができます。私はEWSを使って、 'serviceMail' Inboxの電子メールから添付ファイルを取得するC#プログラムを作成しようとしています。私はアイテムを見つけようとするときに "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)Unauthorized。 " FindItemsResultsのfindResults = service.FindItems(WellKnownFolderName.Inbox、querystring、view)コード行。

アイデア?

答えて

0

あなたのコードは、呼び出し元のアカウントのメールボックスの受信トレイにのみアクセスします。 FolderIdのオーバーロードを使用して、アクセスする実際のメールボックスを指定する必要があります。誤ってダウンレベルフォーマットnetbiosdomain \ユーザー名を使用するか、UPN https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspxを使用し、その場合のドメインを省略しなければならないのいずれか、たとえばあなたはまた、資格情報でユーザー名をspecifingているhttps://msdn.microsoft.com/en-us/library/office/dn641957(v=exchg.150).aspx

の「明示的なアクセスとEWS管理API」を参照してください。 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

関連する問題