Làm việc với các mục Lịch trên máy chủ Exchange
Gửi Yêu cầu Họp
Bài viết này cho thấy cách gửi yêu cầu họp tới nhiều người nhận bằng Exchange Web Services và Aspose.Email.
- Tạo một yêu cầu họp bằng lớp Appointment và đặt vị trí, thời gian và người tham dự.
- Tạo một thể hiện của lớp MailMessage và đặt cuộc hẹn bằng phương thức MailMessage.addAlternateView().
- Kết nối tới máy chủ Exchange và gửi yêu cầu họp bằng phương thức send(MailMessage).
Cái EWSClient lớp có thể được sử dụng để kết nối tới máy chủ Exchange có hỗ trợ Exchange Web Services (EWS). Để hoạt động, máy chủ phải là Exchange Server 2007 hoặc mới hơn. Đoạn mã sau đây cho bạn thấy cách sử dụng EWS để gửi yêu cầu họp.
try {
// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR_OF_DAY, 1);
Date startTime = c.getTime();
c.add(Calendar.MINUTE, 90);
Date endTime = c.getTime();
// Create the meeting request
Appointment app = new Appointment("meeting request", startTime, endTime, MailAddress.to_MailAddress("administrator@test.com"),
MailAddressCollection.to_MailAddressCollection("bob@test.com"));
app.setSummary("meeting request summary");
app.setDescription("description");
c = Calendar.getInstance();
c.add(Calendar.DATE, 5);
RecurrencePattern pattern = new DailyRecurrencePattern(c.getTime());
app.setRecurrence(pattern);
// Create the message and set the meeting request
MailMessage msg = new MailMessage();
msg.setFrom(MailAddress.to_MailAddress("administrator@test.com"));
msg.setTo(MailAddressCollection.to_MailAddressCollection("bob@test.com"));
msg.isBodyHtml(true);
msg.setHtmlBody("<h3>HTML Heading</h3><p>Email Message detail</p>");
msg.setSubject("meeting request");
msg.addAlternateView(app.requestApointment(0));
// send the appointment
client.send(msg);
System.out.println("Appointment request sent");
} catch (java.lang.RuntimeException ex) {
System.out.println(ex.getMessage());
}
Làm việc với các mục Lịch bằng EWS
Aspose.Email cung cấp khả năng thêm, cập nhật và hủy cuộc hẹn bằng cách sử dụng client Exchange Web Service (EWS). Các IEWSClients createAppointment, updateAppointment, và cancelAppointment các phương thức cho phép thao tác các mục lịch bằng EWS. Bài viết này cung cấp mẫu mã chi tiết về làm việc với các mục Lịch. Mẫu mã dưới đây cho thấy cách thực hiện:
- Tạo một cuộc hẹn.
- Cập nhật một cuộc hẹn.
- Xóa/Hủy một cuộc hẹn.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "your.username", "your.Password");
Calendar c = Calendar.getInstance();
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Date startTime = c.getTime();
c.add(Calendar.HOUR_OF_DAY, 1);
Date endTime = c.getTime();
String timeZone = "America/New_York";
Appointment app = new Appointment("Room 112", startTime, endTime, MailAddress.to_MailAddress("organizeraspose-email.test3@domain.com"),
MailAddressCollection.to_MailAddressCollection("attendee@gmail.com"));
app.setTimeZone(timeZone);
app.setSummary("NETWORKNET-34136" + UUID.randomUUID().toString());
app.setDescription("NETWORKNET-34136 Exchange 2007/EWS: Provide support for Add/Update/Delete calendar items");
String uid = client.createAppointment(app);
Appointment fetchedAppointment1 = client.fetchAppointment(uid);
app.setLocation("Room 115");
app.setSummary("New summary for " + app.getSummary());
app.setDescription("New Description");
client.updateAppointment(app);
Appointment[] appointments1 = client.listAppointments();
System.out.println("Total Appointments: " + appointments1.length);
Appointment fetchedAppointment2 = client.fetchAppointment(uid);
System.out.println("Summary: " + fetchedAppointment2.getSummary());
System.out.println("Location: " + fetchedAppointment2.getLocation());
System.out.println("Description: " + fetchedAppointment2.getDescription());
client.cancelAppointment(app);
Appointment[] appointments2 = client.listAppointments();
System.out.println("Total Appointments: " + appointments2.length);
Trả về các mục Lịch định kỳ trong Khoảng ngày được chỉ định
Aspose.Email EWSClient hỗ trợ trả về các mục lịch định kỳ trong phạm vi được chỉ định bởi StartDate và EndDate. The AppointmentQueryBuilder.setCalendarView(Date startDate, Date endDate, int maxEntriesReturned) phương thức, nếu CalendarView được chỉ định, trả về danh sách các mục lịch đơn lẻ và các lần xuất hiện của các mục lịch định kỳ trong phạm vi được xác định bởi StartDate và EndDate. Tham số maxEntriesReturned mô tả số kết quả tối đa. (Giá trị <= 0 cho tất cả kết quả).
ExchangeQueryBuilder builder = new ExchangeQueryBuilder();
builder.getAppointment().setCalendarView(startDate, endDate, -1);
Appointment[] appointments = client.listAppointments(builder.getQuery());
Liệt kê Cuộc hẹn với Hỗ trợ Phân trang
Phương thức ListAppointments được cung cấp bởi IEWSClient API truy xuất danh sách đầy đủ các cuộc hẹn từ máy chủ Exchange. Điều này có thể mất thời gian nếu có số lượng lớn các cuộc hẹn trên máy chủ Exchange. API cung cấp các phương thức overload của listAppointments phương thức cung cấp hỗ trợ phân trang cho thao tác. Điều này cũng có thể được sử dụng trong các kết hợp khác nhau với tính năng truy vấn. Các phương thức overload sau đây có sẵn để liệt kê các cuộc hẹn từ máy chủ Exchange với hỗ trợ Phân trang.
- AppointmentCollection IEWSClient.listAppointments(int itemsPerPage).
- AppointmentCollection IEWSClient.listAppointments(String folderUri, int itemsPerPage).
- AppointmentCollection IEWSClient.listAppointments(MailQuery query, int itemsPerPage).
- AppointmentCollection IEWSClient.listAppointments(String folderUri, MailQuery query, int itemsPerPage).
- AppointmentCollection IEWSClient.listAppointments(int itemsPerPage, int itemOffset).
- AppointmentCollection IEWSClient.listAppointments(String folderUri, int itemsPerPage, int itemOffset).
- AppointmentCollection IEWSClient.listAppointments(MailQuery query, int itemsPerPage, int itemOffset).
- AppointmentCollection IEWSClient.listAppointments(String folderUri, MailQuery query, int itemsPerPage, int itemOffset).
Đoạn mã sau đây cho bạn thấy cách liệt kê các cuộc hẹn với hỗ trợ phân trang.
IEWSClient client = EWSClient.getEWSClient("exchange.domain.com", "username", "password");
try {
try {
Appointment[] appts = client.listAppointments();
System.out.println(appts.length);
Calendar c = Calendar.getInstance();
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
int appNumber = 10;
Map<String, Appointment> appointmentsDict = new HashMap<String, Appointment>();
for (int i = 0; i < appNumber; i++) {
c.set(Calendar.HOUR_OF_DAY, i + 1);
Date startTime = c.getTime();
c.set(Calendar.HOUR_OF_DAY, i + 2);
Date endTime = c.getTime();
String timeZone = "America/New_York";
Appointment appointment = new Appointment("Room 112", startTime, endTime, MailAddress.to_MailAddress("from@domain.com"),
MailAddressCollection.to_MailAddressCollection("to@domain.com"));
appointment.setTimeZone(timeZone);
appointment.setSummary("NETWORKNET-35157_3 - " + UUID.randomUUID().toString());
appointment.setDescription("EMAILNET-35157 Move paging parameters to separate class");
String uid = client.createAppointment(appointment);
appointmentsDict.put(uid, appointment);
}
AppointmentCollection totalAppointmentCol = AppointmentCollection.to_AppointmentCollection(client.listAppointments());
///// LISTING APPOINTMENTS WITH PAGING SUPPORT ///////
int itemsPerPage = 2;
List<AppointmentPageInfo> pages = new ArrayList<AppointmentPageInfo>();
AppointmentPageInfo pagedAppointmentCol = client.listAppointmentsByPage(itemsPerPage);
System.out.println(pagedAppointmentCol.getItems().size());
pages.add(pagedAppointmentCol);
while (!pagedAppointmentCol.getLastPage()) {
pagedAppointmentCol = client.listAppointmentsByPage(itemsPerPage, pagedAppointmentCol.getPageOffset() + 1);
pages.add(pagedAppointmentCol);
}
int retrievedItems = 0;
// foreach to while statements conversion
for (AppointmentPageInfo folderCol : pages) {
retrievedItems += folderCol.getItems().size();
}
} finally {
}
} finally {
client.dispose();
}
Thêm Sự kiện vào Thư mục Lịch phụ trên máy chủ Exchange
API Aspose.Email cho phép bạn tạo một thư mục Lịch phụ trên máy chủ Exchange bằng cách sử dụng IEWSClient. Các cuộc hẹn sau đó có thể được thêm, cập nhật hoặc hủy từ lịch phụ bằng cách sử dụng createAppointment, updateAppointment và cancelAppointment các phương thức. Các phương thức và thuộc tính API sau được sử dụng trong các mẫu mã dưới đây để minh họa chức năng của tính năng này. Lưu ý rằng tính năng này được hỗ trợ bởi Aspose.Email cho Java từ phiên bản 6.5.0 trở lên.
- Phương thức IEWSClient.cancelAppointment(Appointment, String).
- Phương thức IEWSClient.cancelAppointment(String, String).
- Phương thức IEWSClient.createAppointment(Appointment, String).
- Phương thức IEWSClient.createFolder(String, String, ExchangeFolderPermissionCollection, String).
- Phương thức IEWSClient.fetchAppointment(String, String).
- Phương thức IEWSClient.updateAppointment(Appointment, String).
- Thuộc tính IEWSClient.CurrentCalendarFolderUri.
Đoạn mã sau đây cho bạn thấy cách thêm một sự kiện vào thư mục lịch phụ trên máy chủ Exchange.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "your.username", "your.Password");
try {
try {
// Create an appointmenta that will be added to secondary calendar folder
Calendar c = Calendar.getInstance();
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Date startTime = c.getTime();
c.add(Calendar.HOUR_OF_DAY, 1);
Date endTime = c.getTime();
String timeZone = "America/New_York";
Appointment[] listAppointments;
Appointment appointment = new Appointment("Room 121", startTime, endTime, MailAddress.to_MailAddress("from@domain.com"),
MailAddressCollection.to_MailAddressCollection("attendee@domain.com"));
appointment.setTimeZone(timeZone);
appointment.setSummary("EMAILNET-35198 - " + UUID.randomUUID().toString());
appointment.setDescription("EMAILNET-35198 Ability to add event to Secondary Calendar of Office 365");
// Verify that the new folder has been created
ExchangeFolderInfoCollection calendarSubFolders = client.listSubFolders(client.getMailboxInfo().getCalendarUri());
String getfolderName;
String setFolderName = "New Calendar";
boolean alreadyExits = false;
// Verify that the new folder has been created already exits or not
for (int i = 0; i < calendarSubFolders.size(); i++) {
getfolderName = calendarSubFolders.get_Item(i).getDisplayName();
if (getfolderName.equals(setFolderName)) {
alreadyExits = true;
}
}
if (alreadyExits) {
System.out.println("Folder Already Exists");
} else {
// Create new calendar folder
client.createFolder(client.getMailboxInfo().getCalendarUri(), setFolderName, null, "IPF.Appointment");
System.out.println(calendarSubFolders.size());
// Get the created folder URI
String newCalendarFolderUri = calendarSubFolders.get_Item(0).getUri();
// appointment api with calendar folder uri
// Create
client.createAppointment(appointment, newCalendarFolderUri);
appointment.setLocation("Room 122");
// update
client.updateAppointment(appointment, newCalendarFolderUri);
// list
listAppointments = client.listAppointments(newCalendarFolderUri);
// list default calendar folder
listAppointments = client.listAppointments(client.getMailboxInfo().getCalendarUri());
// Cancel
client.cancelAppointment(appointment, newCalendarFolderUri);
listAppointments = client.listAppointments(newCalendarFolderUri);
// appointment api with context current calendar folder uri
client.setCurrentCalendarFolderUri(newCalendarFolderUri);
// Create
client.createAppointment(appointment);
appointment.setLocation("Room 122");
// update
client.updateAppointment(appointment);
// list
listAppointments = client.listAppointments();
// list default calendar folder
listAppointments = client.listAppointments(client.getMailboxInfo().getCalendarUri());
// Cancel
client.cancelAppointment(appointment);
listAppointments = client.listAppointments();
}
} finally {
}
} finally {
client.dispose();
}
Chia sẻ Lời mời Lịch
Máy chủ Microsoft Exchange cung cấp khả năng chia sẻ lịch bằng cách gửi lời mời lịch cho những người dùng khác, đã đăng ký trên cùng một máy chủ Exchange. API Aspose.Email cung cấp cùng khả năng này bằng cách cho phép chia sẻ lịch sử dụng API EWS.
final IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
try {
// delegate calendar access permission
ExchangeDelegateUser delegateUser = new ExchangeDelegateUser("sharingfrom@domain.com", ExchangeDelegateFolderPermissionLevel.NotSpecified);
delegateUser.getFolderPermissions().setCalendarFolderPermissionLevel(ExchangeDelegateFolderPermissionLevel.Reviewer);
client.delegateAccess(delegateUser, "sharingfrom@domain.com");
// Create invitation
MapiMessage mapiMessage = client.createCalendarSharingInvitationMessage("sharingfrom@domain.com");
MailConversionOptions options = new MailConversionOptions();
options.setConvertAsTnef(true);
MailMessage mail = mapiMessage.toMailMessage(options);
client.send(mail);
} finally {
client.dispose();
}
Truy xuất Thông tin Thuộc tính Mở rộng từ các mục Lịch
IEWSClient client = EWSClient.getEWSClient("https://exchange.office365.com/Exchange.asmx", "username", "password");
java.util.List<String> uriList = java.util.Arrays.asList(client.listItems(client.getMailboxInfo().getCalendarUri()));
// Define the Extended Attribute Property Descriptor for searching purpose
// In this case, we have a K1 Long named property for Calendar item
UUID uuid = UUID.fromString("00020329-0000-0000-C000-000000000046");
PropertyDescriptor propertyDescriptor = new PidNamePropertyDescriptor("K1", PropertyDataType.Integer32, uuid);
java.util.List<PropertyDescriptor> propertyDescriptors = java.util.Arrays.asList(new PropertyDescriptor[] { propertyDescriptor });
IGenericList<MapiCalendar> mapiCalendarList = client.fetchMapiCalendar(uriList, propertyDescriptors);
for (MapiCalendar cal : mapiCalendarList) {
for (MapiNamedProperty namedProperty : (Iterable<MapiNamedProperty>) cal.getNamedProperties().getValues()) {
System.out.println(namedProperty.getNameId() + " = " + namedProperty.getInt32());
}
}