1、建立連接:
//與exchange web服務器建立連接 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010); ExchangeCredentials credentials = new WebCredentials([郵箱賬號], [密碼]); service.Credentials = credentials; //這個url可能每個公司不一樣,大多數是這個 service.Url = new Uri("https://outlook.[域名].com/ews/exchange.asmx"); service.TraceEnabled = true;
2、獲取會議列表:
//Initialize values for the start and end times, and the number of appointments to retrieve. DateTime startDate = DateTime.Now.AddDays(-1).Date.AddSeconds(1).AddDays(1); DateTime endDate = startDate.AddDays(1); const int NUM_APPTS = 5; // Initialize the calendar folder object with only the folder ID. CalendarFolder calendar = CalendarFolder.Bind(service, 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, AppointmentSchema.Location, AppointmentSchema.Organizer); // Retrieve a collection of appointments by using the calendar view. FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView); Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() + " to " + endDate.Date.ToShortDateString() + " are: \n"); foreach (Appointment a in appointments) { Console.Write("Subject: " + a.Subject.ToString() + " "); Console.Write("Start: " + a.Start.ToString() + " "); Console.Write("End: " + a.End.ToString()); if (a.Location != null) { Console.Write("End: " + a.Location.ToString()); } Console.WriteLine(); }
3、獲取會議室列表:
try { // 這里先獲取大的會議室列表,大多數是按區域分的,不如公司有五個辦公地點,這里獲取的就是各個辦公地點的address EmailAddressCollection myRoomLists = service.GetRoomLists(); List<String> addList = new List<String>(); foreach (EmailAddress address in myRoomLists) { EmailAddress emailAddress = new EmailAddress(address.Address); // 根據各個辦公地點的address,再獲取具體的所有會議室信息 // 如獲取北京辦公地的所有會議室信息 Collection<EmailAddress> list = service.GetRooms(emailAddress); foreach (EmailAddress e in list) { string aa = e.Address; string bb = e.Name; } } } catch (Exception e1) { // TODO Auto-generated catch block //e1.printStackTrace(); }
4、獲取會議室事件:
//讀取會議室預定情況 List<AttendeeInfo> attendees = new List<AttendeeInfo>(); attendees.Add(new AttendeeInfo("[會議室郵箱地址]", MeetingAttendeeType.Room, true)); GetUserAvailabilityResults results = service.GetUserAvailability(attendees, // 設置當天時間 new TimeWindow(DateTime.Now.AddDays(-3).Date, DateTime.Now.AddDays(1).Date), AvailabilityData.FreeBusyAndSuggestions); List<Hashtable> htList = new List<Hashtable>(); foreach (AttendeeAvailability availability in results.AttendeesAvailability) { foreach (CalendarEvent calEvent in availability.CalendarEvents) { Hashtable ht = new Hashtable(); // 開始時間和結束時間 ht.Add("start", calEvent.StartTime); ht.Add("end", calEvent.EndTime); CalendarEventDetails details = calEvent.Details; if (details != null) { ht.Add("subject", details.Subject); } htList.Add(ht); } }
5、獲取會議室日歷:
CalendarView testCView = new CalendarView(calEvent.StartTime, calEvent.EndTime); FolderId folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox("[會議室郵箱列表]")); FindItemsResults<Appointment> findResults = null; try { findResults = service.FindAppointments(folderId, testCView); List<Appointment> appointmentItems = findResults.Items.ToList(); foreach (Appointment ap in appointmentItems) { string name = ap.Organizer.Name; string address = ap.Organizer.Address; } } catch (Exception e) { }
6、默認返回的Organizer.Address格式為:
/o=[域名]/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=a272ba5365f94925982d3b1a3117444d-[域賬號]
如果要顯示郵箱地址,需要重新加載Organizer.Address屬性值:
//Calendar View CalendarView testCView = new CalendarView(calEvent.StartTime, calEvent.EndTime); FolderId folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox("[會議室郵箱地址]")); FindItemsResults<Appointment> findResults = null; try { findResults = service.FindAppointments(folderId, testCView); PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties); propertySet.RequestedBodyType = BodyType.Text; service.LoadPropertiesForItems(from Item item in findResults select item, propertySet); List<Appointment> appointmentItems = findResults.Items.ToList(); foreach (Appointment ap in appointmentItems) { string name = ap.Organizer.Name; string address = ap.Organizer.Address; } } catch (Exception e) { }
BTW,返回的會議主題可能會變成組織者姓名,與會議室的配置有關系,通過PowerShell命令修改即可,已驗證成功。
Set-CalendarProcessing -Identity [會議室域賬號] -AddOrganizerToSubject $false -DeleteSubject $false
參考文檔:
https://www.slipstick.com/exchange/cmdlets/meeting-organizers-name-appears-in-subject-line/
https://docs.microsoft.com/en-us/powershell/module/exchange/set-calendarprocessing?view=exchange-ps
