I want to create appointment on outlook calendar by using EWS. I followed https://msdn.microsoft.com/en-us/library/office/dn722379(v=exchg.150).aspx link. but I am seeing this error:
"The account does not have permission to impersonate the requested user".
Please go through my code
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);
}
}
}