2017-04-11 2 views
0

다음은 해결하기위한 시나리오입니다. 'myuser'사용자와 'serviceMail'서비스 사서함에 대한 액세스 권한이 있습니다. 그래서 이것은 내 개인 사서함이 아니라 내 회사에서 다른 목적으로 메일을 보낸 다른 사서함 설정입니다. 내 Outlook에이 사서함을 추가했기 때문에 내가 액세스 할 수 있음을 알고 있으며 보통 내 자신의 전자 메일처럼받은 편지함을 확인할 수 있습니다. EWS를 사용하여 'serviceMail'받은 편지함의 전자 메일에서 첨부 파일을 가져올 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) 권한이 없습니다. " 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를 사용하는 경우에는 도메인을 생략해야 하나 예 또한 자격 증명에서 사용자 이름을 명시 할 있습니다 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