使用EWS连接到邮箱时401 UnAuthorized Access

时间:2017-04-11 12:52:56

标签: c# exchangewebservices

以下是我尝试解决的问题:我可以使用我的用户'myuser'访问服务邮箱'serviceMail'。所以这不是我的个人邮箱,而是我公司的另一个邮箱设置,我们已经发送电子邮件用于各种目的。我知道我可以访问它,因为我已经在我的Outlook中添加了这个邮箱,并且我可以像往常一样检查收件箱中的自己的电子邮件。我正在尝试使用EWS编写一个c#程序,该程序将从“serviceMail”收件箱中的电子邮件中获取附件。我在尝试查找项目时收到“401 Unauthroized Access”。我究竟做错了什么?我的代码如下。

以下是我连接服务的方式:

  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)代码行。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的代码只会访问主叫帐户邮箱的收件箱。您需要使用FolderId重载来指定要访问的实际邮箱。请参阅&#34;显式访问和EWS托管API&#34;在https://msdn.microsoft.com/en-us/library/office/dn641957(v=exchg.150).aspx

您还在错误地指定了凭据中的用户名,例如,您应该使用下层格式netbiosdomain \ username或使用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

相关问题