You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EWS v15.0.0.0在.NET MVC发会议邀请遇401未授权错误求助

使用EWS发送会议邀请时遇到401未授权错误

我尝试使用EWS(Exchange Web Services)发送会议邀请,但一直报错:

"The request failed. The remote server returned an error: (401) Unauthorized."

我确认提供的凭据是有效的,但错误依旧。以下是相关代码,请求协助解决该错误以成功发送会议邀请。

Ajax调用代码

SendInvite: function () {
        debugger;
        $.ajax({
            url: _WebApiUrl + "ExchangeServer/SendAppointment?AdminAccount=" + $('#AdminAccount').val() + "&Password=" + $('#Password').val() + "&Domain=" + $('#Domain').val() + "&EWSUrl=" + $('#Url').val() + "&Attendees=" + $('#Attendees').val(),
            type: "POST",
            headers: { "Authorization": "Bearer " + _StorageUtility.GetAccessToken() }, // Add the Authorization header
            success: function (output, status, xhr) {
                debugger;
                console.log(output);
                _LoaderUtility.Loading(false);
            },
            error: function (xhr, status, err) {
                _LoaderUtility.Loading(false);
                console.log('AJAX request failed. Status: ' + status + ', Error: ' + err);
            }
        });
    },

控制器方法代码

[HttpPost]
public string SendAppointment(string AdminAccount,string Password,string Domain,string EWSUrl,string Attendees)
{
    string data = "";
    try
    {
        ExchangeService service = new ExchangeService();
        service.Credentials = new WebCredentials(AdminAccount, Password, Domain);
        service.Url = new Uri(EWSUrl);

        data += "AdminAccount : " + AdminAccount + " ";
        data += "Password : " + Password + "";
        data += "Domain : " + Domain + " ";
        data += "EWSUrl : " + EWSUrl + " ";
        data += "Attendees : " + Attendees + " ";

        Appointment meeting = new Appointment(service);

        //Set the properties on the meeting object to create the meeting.
        meeting.Subject = "Team building exercise";
        meeting.Body = "Let's learn to really work as a team and then have lunch!";
        meeting.Start = DateTime.Now.AddDays(2);
        meeting.End = meeting.Start.AddHours(4);
        meeting.Location = "Conference Room 12";

        string[] attendeeEmails = Attendees.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var email in attendeeEmails)
        {
            string trimmedEmail = email.Trim();
            if (!string.IsNullOrEmpty(trimmedEmail))
            {
                meeting.RequiredAttendees.Add(trimmedEmail);
            }
        }

        meeting.ReminderMinutesBeforeStart = 60;

        //Save the meeting to the Calendar folder and send the meeting request.
        meeting.Save(SendInvitationsMode.SendToAllAndSaveCopy);

        //Verify that the meeting was created.
        Item item = Item.Bind(service, meeting.Id, new PropertySet(ItemSchema.Subject));

        //return Ok("Meeting created: " + item.Subject);

        data += " Meeting created : " + item.Subject + " ";
    }
    catch (Exception ex)
    {
        data += " Exception Msg: " + ex.Message  + " StackTrace = " + ex.StackTrace ;
    }
    return data;
}

异常详情

EWS Editor捕获的异常:

异常信息: 请求失败。远程服务器返回错误: (401) 未授权。
类型: Microsoft.Exchange.WebServices.Data.ServiceRequestException
调用栈:
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder(FolderId folderId, PropertySet propertySet)
at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder[TFolder](FolderId folderId, PropertySet propertySet)
at Microsoft.M365.RCA.ConnectivityTests.GetOrCreateSyncFolderTest.PerformTestReally()

异常信息: 远程服务器返回错误: (401) 未授权。
类型: System.Net.WebException
调用栈:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- 之前抛出异常的堆栈跟踪结束 ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.d__67.MoveNext()
--- 之前抛出异常的堆栈跟踪结束 ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)

耗时: 970 ms.

内容的提问来源于stack exchange,提问作者Ajisha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 20:32:53