[C#]使用 Exchange Web Services (EWS) Managed API 存取 Outlook的行事曆

  • 1757
  • 0
  • 2016-06-16

用程式讀取 Outlook 行事曆項目

1. 在Visual Studio 的「套件管理器主控台」執行 “Install-Package Microsoft.Exchange.WebServices”,安裝相關套件

2. C# 程式範例:
 

ExchangeService es = new ExchangeService(ExchangeVersion.Exchange2007_SP1);//版本預設值最新版
es.Credentials = new WebCredentials("帳號", "密碼", "網域");
es.Url = new Uri("http://server/EWS/Exchange.asmx"); // Server路徑

DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(30);
const int NUM_APPTS = 5;

// Initialize the calendar folder object with only the folder ID. 
CalendarFolder calendar = CalendarFolder.Bind(es, WellKnownFolderName.Calendar, new PropertySet());

// Set the start and end time and number of appointments to retrieve.
CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);

// Limit the properties returned to the appointment's subject, start time, and end time.
cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

// Retrieve a collection of appointments by using the calendar view.
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

string str = "\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() +
            " to " + endDate.Date.ToShortDateString() + " are: \n";

foreach (Appointment a in appointments)
{
    string str1 = "Subject: " + a.Subject.ToString() + " " + Environment.NewLine
                + "Start: " + a.Start.ToString() + " " + Environment.NewLine
                + "End: " + a.End.ToString() + Environment.NewLine ;

    textBox1.Text += str1;
}