在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
枚舉值。