Làm việc với các cuộc hẹn
Tải và Lưu một cuộc hẹn ở định dạng ICS
Cái Appointment lớp trong Aspose.Email cho Java có thể được dùng để tải một cuộc hẹn ở định dạng ICS cũng như tạo một cuộc hẹn mới và lưu nó vào đĩa ở định dạng ICS. Trong bài viết này, chúng ta sẽ tạo một cuộc hẹn và lưu nó vào đĩa ở định dạng ICS rồi tải lại.
Tải một cuộc hẹn ở định dạng ICS
Để tải một cuộc hẹn ở định dạng ICS, cần thực hiện các bước sau:
- Tạo một đối tượng của Appointment lớp.
- Gọi load() phương thức, cung cấp đường dẫn của tệp ICS.
- Đọc các thuộc tính của nó để lấy thông tin từ cuộc hẹn (tệp ICS).
Đoạn mã sau cho thấy cách tải một cuộc hẹn ở định dạng ICS.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
// Load the appointment from an ICS file
Appointment appointment = Appointment.load(dataDir + "appointment.ics");
// Read its properties
System.out.println("Summary: " + appointment.getSummary());
System.out.println("Location: " + appointment.getLocation());
System.out.println("Description: " + appointment.getDescription());
System.out.println("Start date: " + appointment.getStartDate());
System.out.println("End date: " + appointment.getEndDate());
System.out.println("Organizer: " + appointment.getOrganizer());
System.out.println("Attendees: " + appointment.getAttendees());
Tạo một cuộc hẹn và lưu vào đĩa ở định dạng ICS
Cần thực hiện các bước sau để tạo một cuộc hẹn và lưu nó ở định dạng ICS.
- Tạo một đối tượng của Appointment lớp, truyền vị trí, tóm tắt, mô tả, ngày bắt đầu, ngày kết thúc, người tổ chức và người tham dự vào constructor của nó.
- Đặt ngày tạo và ngày sửa đổi cuối cùng nếu bạn cần chúng trong đầu ra.
- Gọi save() phương thức, chỉ định tên tệp và định dạng.
Cuộc hẹn có thể được mở trong Microsoft Outlook hoặc bất kỳ chương trình nào có thể tải tệp ICS. Nếu tệp được mở trong Microsoft Outlook, nó sẽ tự động thêm cuộc hẹn vào lịch Outlook.
Đoạn mã sau cho thấy cách tạo một cuộc hẹn và lưu nó vào đĩa ở định dạng ICS.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
// Set the start and end date of the appointment
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 10, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 11, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("attendee@domain.com", "Attendee"));
// Location, summary, description, start, end, organizer and attendees
Appointment appointment = new Appointment(
"Room 112",
"Monthly Meeting",
"Please confirm your attendance.",
startDate,
endDate,
new MailAddress("organizer@domain.com", "Organizer"),
attendees);
appointment.setCreatedDate(new Date());
appointment.setLastModifiedDate(new Date());
// Save it in ICS format
appointment.save(dataDir + "appointment_out.ics", AppointmentSaveFormat.Ics);
Lưu các cuộc hẹn sang định dạng MSG
Aspose.Email cho phép lưu các cuộc hẹn trực tiếp thành tệp .msg. Các lớp công cộng sau đây có sẵn để tùy chỉnh cách lưu các cuộc hẹn:
- AppointmentMsgSaveOptions class với các tùy chọn bổ sung để lưu các cuộc hẹn ở định dạng msg.
- AppointmentIcsSaveOptions lớp với các tùy chọn bổ sung để lưu một cuộc hẹn ở định dạng ICS. Nó thay thế
IcsSaveOptions.
Đoạn mã mẫu dưới đây cho thấy cách tải một cuộc hẹn từ tệp, và sau đó lưu nó ở hai định dạng khác nhau: .ics và .msg.
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "appointment.ics");
appointment.save(dataDir + "appointment_out.ics", new AppointmentIcsSaveOptions());
appointment.save(dataDir + "appointment_out.msg", new AppointmentMsgSaveOptions());
Tạo một cuộc hẹn với nội dung HTML
Đó là thực tiễn phổ biến để sử dụng tiêu đề X-ALT-DESC trong định dạng iCalendar (RFC 5545). Đây là thuộc tính mở rộng cung cấp mô tả thay thế có thể đọc được của một mục lịch hoặc sự kiện. Tiêu đề này thường được dùng để bao gồm một bản văn bản thuần hoặc HTML của mô tả sự kiện, hữu ích cho tính tương thích với phần mềm lịch cũ hơn hoặc để cung cấp phiên bản mô tả đơn giản hơn. Trong các trường hợp mô tả chính không được hỗ trợ hoặc hiển thị đúng bởi ứng dụng lịch của người nhận, tiêu đề X-ALT-DESC được dùng để cung cấp mô tả thay thế cho sự kiện. Nó cho phép người gửi bao gồm các biểu diễn khác nhau của mô tả sự kiện để đảm bảo khả năng tương thích và truy cập tốt hơn trên các phần mềm và nền tảng lịch khác nhau. Để tạo một cuộc hẹn với nội dung HTML, truyền markup vào setHtmlDescription phương thức. Đoạn mã mẫu dưới đây tạo một cuộc hẹn với ngày, giờ, địa điểm, người tổ chức, người tham dự và mô tả đã định dạng:
Date startDate = new Date();
// One hour later
Date endDate = new Date(startDate.getTime() + 60 * 60 * 1000);
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("AinaMartensson@to.com", "Aina Martensson"));
Appointment appointment = new Appointment("Bygget 83",
startDate, // start date
endDate, // end date
new MailAddress("TintinStrom@from.com", "Tintin Strom"), // organizer
attendees); // attendees
appointment.setHtmlDescription("<html>\n"
+ " <style type=\"text/css\">\n"
+ " .text {\n"
+ " font-family:'Comic Sans MS';\n"
+ " font-size:16px;\n"
+ " }\n"
+ " </style>\n"
+ " <body>\n"
+ " <p class=\"text\">Hi, I'm happy to invite you to our party.</p>\n"
+ " </body>\n"
+ " </html>");
Tạo yêu cầu cuộc hẹn bản nháp
Để lưu một cuộc hẹn ở chế độ nháp, loại phương thức của Appointment lớp nên được đặt thành Publish với setMethodType. Lưu ý tên accessor: trong Aspose.Email cho Java nó là setMethodType, không phải setMethod. Đoạn mã mẫu sau minh họa cách sử dụng.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
String sender = "sender@domain.com";
String recipient = "recipient@domain.com";
MailMessage message = new MailMessage(sender, recipient, "", "");
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.DECEMBER, 1, 0, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("attendee@domain.com", "Attendee"));
Appointment app = new Appointment("", startDate, endDate,
new MailAddress("organizer@domain.com", "Organizer"), attendees);
// Publish marks the appointment as a draft rather than a request
app.setMethodType(AppointmentMethodType.Publish);
message.addAlternateView(app.requestApointment());
MapiMessage msg = MapiMessage.fromMailMessage(message);
// Save the appointment as a draft
msg.save(dataDir + "appointment-draft.msg");
Tạo cuộc hẹn bản nháp từ văn bản
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
String ical = "BEGIN:VCALENDAR\r\n"
+ "METHOD:PUBLISH\r\n"
+ "PRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN\r\n"
+ "VERSION:2.0\r\n"
+ "BEGIN:VEVENT\r\n"
+ "ATTENDEE;CN=attendee@domain.com:mailto:attendee@domain.com\r\n"
+ "DTSTART:20130220T171439\r\n"
+ "DTEND:20130220T174439\r\n"
+ "DTSTAMP:20130220T161439Z\r\n"
+ "END:VEVENT\r\n"
+ "END:VCALENDAR";
MailMessage message = new MailMessage("sender@domain.com", "recipient@domain.com", "", "");
AlternateView av = AlternateView.createAlternateViewFromString(ical, new ContentType("text/calendar"));
message.getAlternateViews().addItem(av);
MapiMessage msg = MapiMessage.fromMailMessage(message);
// Save the appointment as a draft
msg.save(dataDir + "DraftAppointment.msg");
Thêm và Xóa tệp đính kèm từ các mục lịch
Aspose.Email cung cấp một bộ sưu tập tệp đính kèm có thể được dùng để thêm và lấy các tệp đính kèm liên quan tới mục lịch. Bài viết này cho thấy cách:
- Tạo và thêm tệp đính kèm vào một Appointment đối tượng class.
- Lấy thông tin các tệp đính kèm từ một cuộc hẹn.
- Trích xuất tệp đính kèm từ một cuộc hẹn.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the resource directory.
String dataDir = "data/";
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.DECEMBER, 1, 0, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("attendee@domain.com", "Attendee"));
WeeklyRecurrencePattern recurrence = new WeeklyRecurrencePattern(3);
Appointment app = new Appointment("Appointment Location", "Appointment Summary",
"Appointment Description", startDate, endDate,
new MailAddress("organizer@domain.com", "Organizer"), attendees, recurrence);
// Attach a file from disk to this appointment
File file = new File(dataDir + "sample.xlsx");
try (FileInputStream fis = new FileInputStream(file)) {
app.getAttachments().addItem(new Attachment(fis, file.getName()));
}
String savedFile = "appWithAttachments.ics";
app.save(dataDir + savedFile, AppointmentSaveFormat.Ics);
// Load it back and extract the attachments
Appointment loaded = Appointment.load(dataDir + savedFile);
System.out.println("Total Attachments: " + loaded.getAttachments().size());
for (int i = 0; i < loaded.getAttachments().size(); i++) {
Attachment att = loaded.getAttachments().get_Item(i);
System.out.println(att.getName());
// Save the attachment to disk
att.save(dataDir + att.getName());
}
Định dạng các cuộc hẹn
Các mẫu lập trình dưới đây minh họa cách sử dụng AppointmentFormattingOptions class để định dạng văn bản và HTML.
Mẫu lập trình - Định dạng văn bản
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "test.ics");
AppointmentFormattingOptions formattingOptions = new AppointmentFormattingOptions();
formattingOptions.setLocationFormat("Where: {0}");
formattingOptions.setTitleFormat("Subject: {0}");
formattingOptions.setDescriptionFormat("\r\n*~*~*~*~*~*~*~*~*~*\r\n{0}");
System.out.println(appointment.getAppointmentText(formattingOptions));
Mẫu lập trình - Định dạng HTML
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "test.ics");
AppointmentFormattingOptions formattingOptions = AppointmentFormattingOptions.createAsHtml();
formattingOptions.setLocationFormat("<FONT SIZE=2 FACE=\"Arial\"><b>Where: {0}</b></FONT><BR>");
formattingOptions.setTitleFormat("<FONT SIZE=2 FACE=\"Arial\"><b>Subject: {0}</b></FONT><BR>");
formattingOptions.setDescriptionFormat("<P><FONT SIZE=2 FACE=\"Arial\">-----------<br><i>{0}</i></FONT></P>");
System.out.println(appointment.getAppointmentText(formattingOptions));
Chuyển đổi ICS sang MSG
API cho phép bạn dễ dàng chuyển một Appointment thành một đối tượng tin nhắn. Đoạn mã mẫu sau cho thấy cách chuyển yêu cầu cuộc hẹn thành MailMessage hoặc MapiMessage:
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "appRequest.ics");
MailMessage eml = appointment.toMailMessage();
MapiMessage msg = appointment.toMapiMessage();
msg.save(dataDir + "appRequest.msg");
Xác định phiên bản cuộc hẹn
Để xác định phiên bản của một cuộc hẹn, sử dụng getVersion() phương thức của Appointment lớp. Nó giúp xác định phiên bản mà các tệp dựa trên, đảm bảo tích hợp với các hệ thống và ứng dụng khác.
Đoạn mã mẫu sau cho thấy cách sử dụng phương thức này trong dự án của bạn:
Appointment app = Appointment.load("data/meeting.ics");
// The version is a string that holds the ICS/VCS version, e.g. "2.0" for iCalendar
if ("2.0".equals(app.getVersion())) {
// do something
}
Đọc nhiều sự kiện từ tệp ICS
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
List<Appointment> appointments = new ArrayList<Appointment>();
CalendarReader reader = new CalendarReader(dataDir + "US-Holidays.ics");
while (reader.nextEvent()) {
appointments.add(reader.getCurrent());
}
System.out.println("Number of events read: " + appointments.size());
// Reading events from a specific index
appointments = new ArrayList<Appointment>();
AppointmentLoadOptions options = new AppointmentLoadOptions();
options.setEventIndex(4);
// Start reading from the fifth event in the file
reader = new CalendarReader(dataDir + "US-Holidays.ics", options);
while (reader.nextEvent()) {
appointments.add(reader.getCurrent());
}
System.out.println("Number of events read from index 4: " + appointments.size());
Viết Nhiều Sự Kiện vào Tệp ICS
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
AppointmentIcsSaveOptions saveOptions = new AppointmentIcsSaveOptions();
saveOptions.setAction(AppointmentAction.Create);
CalendarWriter writer = new CalendarWriter(dataDir + "WriteMultipleEventsToICS_out.ics", saveOptions);
// Set the start and end date of the meeting
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.JUNE, 19, 19, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.JUNE, 19, 20, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("recipient@domain.com"));
try {
for (int i = 0; i < 10; i++) {
Appointment app = new Appointment(
"Room 112", // location
startDate, // start time
endDate, // end time
new MailAddress("organizer@domain.com"), // organizer
attendees); // attendees
app.setDescription("Test body " + i);
app.setSummary("Test summary: " + i);
writer.write(app);
}
} finally {
writer.dispose();
}
Đặt trạng thái người tham dự của cuộc hẹn
Aspose.Email cho Java cho phép bạn đặt trạng thái của các người tham dự cuộc hẹn khi soạn thư trả lời. Điều này sẽ thêm thuộc tính PARTSTAT vào tệp ICS.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String location = "Room 5";
java.util.Calendar c = java.util.Calendar.getInstance();
c.set(2026, java.util.Calendar.DECEMBER, 10, 10, 12, 11);
Date startDate = c.getTime();
c.set(2026, java.util.Calendar.DECEMBER, 10, 13, 11, 12);
Date endDate = c.getTime();
MailAddress organizer = new MailAddress("organizer@domain.com", "Organizer");
MailAddress attendee1 = new MailAddress("first@domain.com", "First attendee");
MailAddress attendee2 = new MailAddress("second@domain.com", "Second attendee");
attendee1.setParticipationStatus(ParticipationStatus.Accepted);
attendee2.setParticipationStatus(ParticipationStatus.Declined);
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(attendee1);
attendees.addMailAddress(attendee2);
Appointment target = new Appointment(location, startDate, endDate, organizer, attendees);
Tùy chỉnh định danh sản phẩm cho ICalendar
Aspose.Email cho Java cho phép bạn lấy hoặc đặt định danh sản phẩm đã tạo đối tượng iCalendar. Nó có thể được đặt trên chính cuộc hẹn, hoặc trên AppointmentIcsSaveOptions khi lưu.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "appointment.ics");
// Read the product identifier that created the iCalendar object
System.out.println("Product ID: " + appointment.getProductId());
// The identifier written to the output file can be set on the appointment ...
appointment.setProductId("-//My Company//My Product//EN");
// ... or on the save options
AppointmentIcsSaveOptions saveOptions = new AppointmentIcsSaveOptions();
saveOptions.setProductId("-//My Company//My Product//EN");
appointment.save(dataDir + "appointment_out.ics", saveOptions);
Cách khắc phục lỗi xác thực địa chỉ khi cố gắng tải các cuộc hẹn
Aspose.Email cho Java cho phép bạn vượt qua lỗi xác thực email bằng cách thiết lập IgnoreSmtpAddressCheck tùy chọn trên AppointmentLoadOptions đối tượng và truyền nó vào lời gọi load.
AppointmentLoadOptions lo = new AppointmentLoadOptions();
lo.setIgnoreSmtpAddressCheck(true);
Appointment appointment = Appointment.load("data/app.ics", lo);