使用EWS连接到邮箱时的401未经授权访问 [英] 401 UnAuthorized Access when using EWS to connect to mailbox

查看:105
本文介绍了使用EWS连接到邮箱时的401未经授权访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要解决的方案:我的用户"myuser"可以访问服务邮箱"serviceMail".因此,这不是我的个人邮箱,而是我公司的另一种邮箱设置,我们已将其发送给各种目的的电子邮件.我知道我可以使用它,因为我已经在Outlook中添加了此邮箱,并且能够像平常一样检查收件箱中自己的电子邮件.我正在尝试使用EWS编写一个c#程序,该程序将从'serviceMail'收件箱中的电子邮件中获取附件.尝试查找项目时出现"401未经授权的访问".我究竟做错了什么?我的代码在下面.

Here is my scenario that I am trying to solve: I have access with my user 'myuser' to a service mailbox 'serviceMail'. So this is not my personal mailbox but another mailbox setup at my company that we have email sent to for various purposes. I know I have access to it, because I have added this mailbox in my Outlook and I am able to check the inbox like I normally for my own email. I am trying to use EWS to write a c# program that will grab the attachments from emails in the 'serviceMail' Inbox. I am getting an "401 Unauthroized Access" when attempting to find items. What am I doing wrong? My code is below.

这是我连接到该服务的方式:

Here is how I am connecting to the service:

  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;
    }

下面是尝试查找项目的代码

below is code for trying to find the items

  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)的代码行上.

I get the error "The request failed. The remote server returned an error: (401) Unauthorized." on the FindItemsResults findResults = service.FindItems(WellKnownFolderName.Inbox, querystring, view) line of code.

有什么想法吗?

推荐答案

您的代码将仅访问呼叫帐户邮箱的收件箱.您需要使用FolderId重载来指定要访问的实际邮箱.请参见

Your code will only access the Inbox of the mailbox of the calling account. You need to use FolderId overload to specify the actual mailbox you want to access. see "Explicit access and the EWS Managed API" in https://msdn.microsoft.com/en-us/library/office/dn641957(v=exchg.150).aspx

您还错误地在凭据中指定了用户名,例如,您应该使用下层格式netbiosdomain \ username或使用UPN https://social.technet.microsoft.com/Forums/zh-CN/12de5368-dde0-4d91-a1b2-394c4487d0f1/ews-the请求失败的远程服务器返回了未经授权的错误401?forum = exchangesvrdevelopment

You are also specifing the username in the credentials incorrectly eg you should either use the downlevel format netbiosdomain\username or use the UPN https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspx and omit the domain in that case. see 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

这篇关于使用EWS连接到邮箱时的401未经授权访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆