2017-04-11 77 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

相关问题