在C#中,使用Exchange Web Services (EWS) 创建日历事件需要以下步骤:
安装EWS Managed API。可以通过NuGet包管理器或从Microsoft官方网站下载并安装。
添加引用。在项目中添加对Microsoft.Exchange.WebServices.dll
的引用。
编写代码。以下是一个简单的示例,展示了如何使用EWS Managed API创建日历事件:
using System;
using Microsoft.Exchange.WebServices.Data;
namespace EwsCreateCalendarEvent
{
class Program
{
static void Main(string[] args)
{
// Exchange服务器的URL
string exchangeUrl = "https://your-exchange-server/ews/exchange.asmx";
// 用户的电子邮件地址和密码
string userEmail = "user@example.com";
string userPassword = "your_password";
// 创建ExchangeService对象
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new WebCredentials(userEmail, userPassword);
service.Url = new Uri(exchangeUrl);
// 创建日历事件
Appointment appointment = new Appointment(service);
appointment.Subject = "My Calendar Event";
appointment.Body = "This is a test event.";
appointment.Start = DateTime.Now.AddHours(1);
appointment.End = DateTime.Now.AddHours(2);
appointment.Location = "Conference Room A";
appointment.ReminderMinutesBeforeStart = 15;
// 保存日历事件
appointment.Save(SendInvitationsMode.SendToNone);
Console.WriteLine("Calendar event created successfully.");
}
}
}
请确保将exchangeUrl
、userEmail
和userPassword
替换为实际的Exchange服务器URL和用户凭据。此示例将在当前时间的1小时后创建一个名为"My Calendar Event"的日历事件,持续1小时,位于"Conference Room A"。
注意:这个示例仅适用于Exchange 2010 SP2及更高版本。如果你使用的是较旧版本的Exchange,请根据需要调整ExchangeVersion
枚举值。