我正在开发一个 Web 应用程序,其中一个功能是将 Web 应用程序中的事件/约会设置为登录该应用程序的用户的 Outlook 日历。我正在使用 EWS 托管 API 来涵盖此功能。这在 localhost 上运行良好,但是当我在 IIS 上部署应用程序时,约会/事件不会在 Outlook 日历中创建。我已经对此进行了搜索,并且可能由于某些权限而发生。EWS 是实现上述功能的正确 API,还是我走错路了?
请提出任何解决方案。
EWS 代码
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
{
Mailbox primary = new Mailbox(emailAddress);
Microsoft.Exchange.WebServices.Data.Folder primaryCalendar = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, new FolderId(WellKnownFolderName.Calendar, primary));
appointment.Save(primaryCalendar.Id, SendInvitationsMode.SendToAllAndSaveCopy);
}
catch (ServiceResponseException ex)
{
Console.WriteLine(string.Format("Could not create appointment for {0}", emailAddress));
Console.WriteLine(ex.Message);
}
}
}