该帐户无权在其他用户的Outlook日历上保存约会时模拟请求的用户

时间:2017-01-06 17:18:33

标签: c# exchangewebservices

我想使用EWS在Outlook日历上创建约会。我关注https://msdn.microsoft.com/en-us/library/office/dn722379(v=exchg.150).aspx链接。但我看到了这个错误:

  

"该帐户无权模拟所请求的用户"。

请查看我的代码

private static ExchangeService Service
    {
        get
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
            //service.Credentials = new WebCredentials("example@abc.com", password);
            service.AutodiscoverUrl("example@abc.com");
            return service;
        }
    }

private void SaveAppointment()
{
        IAppointmentFactory iappointment = new AppointmentFactory();
        List<string> lstEmail = new List<string>() {"other@abc.com"};

        CreateAppointments(Service, lstEmail, iappointment);
}

private static void CreateAppointments(ExchangeService service, List<string> emailAddresses, IAppointmentFactory factory)
{
    // Loop through the list of email addresses to add the appointment.
    foreach (var emailAddress in emailAddresses)
    {
        Console.WriteLine(string.Format("  Placing appointment in calendar for {0}.", emailAddress));

        // Set the email address of the account to get the appointment.
        service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

        // Get the appointment to add.
        Microsoft.Exchange.WebServices.Data.Appointment appointment = factory.GetAppointment(service);

        // Save the appointment.
        try
        {
            if (appointment.RequiredAttendees.Count > 0)
            {
                // The appointment has attendees so send them the meeting request.
                appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
            }
            else
            {
                // The appointment does not have attendees, so just save to calendar.
                appointment.Save(SendInvitationsMode.SendToNone);
            }
        }
        catch (ServiceResponseException ex)
        {
            Console.WriteLine(string.Format("Could not create appointment for {0}", emailAddress));
            Console.WriteLine(ex.Message);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

似乎您只有委托访问日历,您希望将约会保存到。

为了让您的代码正常运行,请删除

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

并更改Save方法,如下所示:

appointment.Save(new FolderId(WellKnownFolderName.Calendar, new Mailbox(emailAddress)), SendInvitationsMode.SendOnlyToAllAndSaveCopy);