Quản lý Cuộc Hẹn: Tạo, Tải, Chuyển Đổi ICS và Làm Việc với Lặp Lại
Lời mời họp, xuất khẩu lịch và dữ liệu lập lịch đều di chuyển dưới dạng tệp iCalendar (ICS). Trong Aspose.Email cho C++ một VEVENT đơn được đại diện bởi Appointment lớp của Aspose::Email::Calendar không gian tên; các quy tắc lặp lại tồn tại trong Aspose::Email::Calendar::Recurrences. Bài viết này gắn liền với phía tiêu chuẩn của việc lên lịch. Outlook-đặc thù MapiCalendar các mục được bao phủ trong Làm việc với các mục Lịch Outlook, và các mục lịch được lưu trong một PST trong Làm việc với mục Lịch trong tệp PST.
Các mẫu dưới đây sử dụng các tiêu đề và không gian tên sau:
#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <AlternateView.h>
#include <AlternateViewCollection.h>
#include <ParticipationStatus.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <Mime/ContentType.h>
#include <Mapi/MapiMessage.h>
#include <Calendar/Appointment.h>
#include <Calendar/AppointmentIcsSaveOptions.h>
#include <Calendar/AppointmentMsgSaveOptions.h>
#include <Calendar/AppointmentMethodType.h>
#include <Calendar/AppointmentAction.h>
#include <Calendar/CalendarReader.h>
#include <Calendar/CalendarWriter.h>
#include <Calendar/Recurrences/CalendarRecurrence.h>
#include <Calendar/Recurrences/RecurrenceRule.h>
#include <Calendar/Recurrences/RecurrenceRuleCollection.h>
#include <Calendar/Recurrences/ByNumberCollection.h>
#include <Calendar/Recurrences/DateCollection.h>
#include <Calendar/Recurrences/WeeklyRecurrencePattern.h>
#include <Calendar/Recurrences/CalendarDay.h>
#include <Calendar/Recurrences/Frequency.h>
#include <system/guid.h>
#include <system/collections/list.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Mime;
using namespace Aspose::Email::Calendar;
using namespace Aspose::Email::Calendar::Recurrences;
Tạo Một Cuộc Hẹn và Lưu Nó dưới Dạng ICS hoặc MSG
Bảy-đối-số Appointment trình khởi tạo nhận vị trí, tóm tắt, mô tả, thời gian bắt đầu và kết thúc, người tổ chức là một MailAddress và các người tham dự như một MailAddressCollection. Lưu ghi kết quả theo định dạng được chọn bởi đối tượng tùy chọn: AppointmentIcsSaveOptions cho một tệp ICS mà bất kỳ ứng dụng lịch nào nhập, hoặc AppointmentMsgSaveOptions cho một mục MSG Outlook.
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"attendee@domain.com"));
SharedPtr<Appointment> appointment = MakeObject<Appointment>(
u"Meeting Room 3 at Office Headquarters", // location
u"Monthly Meeting", // summary
u"Please confirm your availability.", // description
DateTime(2025, 2, 8, 13, 0, 0), // start date
DateTime(2025, 2, 8, 14, 0, 0), // end date
MakeObject<MailAddress>(u"organizer@domain.com"), // organizer
attendees); // attendees
// iCalendar (ICS) output
appointment->Save(u"MonthlyMeeting.ics", MakeObject<AppointmentIcsSaveOptions>());
// Outlook MSG output
appointment->Save(u"MonthlyMeeting.msg", MakeObject<AppointmentMsgSaveOptions>());
Thêm Mô Tả HTML
iCalendar cho phép một cách hiển thị thay thế, giàu hơn cho mô tả thông qua X-ALT-DESC thuộc tính. Gán markup cho HtmlDescription; các máy khách hiểu thuộc tính sẽ hiển thị HTML, phần còn lại sẽ quay lại bản thuần văn bản. DESCRIPTION. Trình khởi tạo ngắn hơn năm đối số được dùng ở đây bỏ qua tóm tắt và mô tả.
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"AinaMartensson@to.com", String(u"Aina Martensson")));
SharedPtr<Appointment> appointment = MakeObject<Appointment>(
u"Bygget 83",
DateTime::get_UtcNow(), // start date
DateTime::get_UtcNow().AddHours(1), // end date
MakeObject<MailAddress>(u"TintinStrom@from.com", String(u"Tintin Strom")), // organizer
attendees);
// alternative representation of the description, written as X-ALT-DESC
appointment->set_HtmlDescription(
u"<html>"
u"<style type=\"text/css\">.text { font-family:'Comic Sans MS'; font-size:16px; }</style>"
u"<body><p class=\"text\">Hi, I'm happy to invite you to our party.</p></body>"
u"</html>");
appointment->Save(u"PartyInvitation.ics", MakeObject<AppointmentIcsSaveOptions>());
Tạo Yêu Cầu Họp Nháp
Yêu cầu họp là một cuộc hẹn được nhúng trong một tin nhắn email như một text/calendar chế độ xem thay thế. Để giữ nó như một bản nháp có thể hoàn thiện sau - trong Outlook, ví dụ - đặt MethodType đến AppointmentMethodType::Publish, đính kèm chế độ xem được tạo bởi RequestApointment tới một MailMessage, chuyển tin nhắn sang MAPI và lưu nó dưới dạng MSG.
String sender = u"test@gmail.com";
String recipient = u"test@email.com";
SharedPtr<MailMessage> message = MakeObject<MailMessage>(sender, recipient, String::Empty, String::Empty);
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(recipient));
SharedPtr<Appointment> appointment = MakeObject<Appointment>(String::Empty, DateTime::get_Now(), DateTime::get_Now(),
MakeObject<MailAddress>(sender), attendees);
// PUBLISH marks the request as a draft that can be completed later
appointment->set_MethodType(AppointmentMethodType::Publish);
message->AddAlternateView(appointment->RequestApointment());
SharedPtr<MapiMessage> msg = MapiMessage::FromMailMessage(message);
msg->Save(u"DraftAppointment.msg");
Tạo Bản Nháp từ Văn Bản iCalendar
Nếu lời mời đã tồn tại dưới dạng văn bản iCalendar - nhận được từ hệ thống khác, hoặc được tạo bởi mẫu - nó có thể được đóng gói vào một AlternateView với CreateAlternateViewFromString và một text/calendar ContentType, sau đó được lưu cùng cách.
String ical =
u"BEGIN:VCALENDAR\r\n"
u"METHOD:PUBLISH\r\n"
u"PRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN\r\n"
u"VERSION:2.0\r\n"
u"BEGIN:VEVENT\r\n"
u"ATTENDEE;CN=test@gmail.com:mailto:test@gmail.com\r\n"
u"DTSTART:20130220T171439\r\n"
u"DTEND:20130220T174439\r\n"
u"DTSTAMP:20130220T161439Z\r\n"
u"END:VEVENT\r\n"
u"END:VCALENDAR";
SharedPtr<MailMessage> message = MakeObject<MailMessage>(u"test@gmail.com", u"test@email.com", String::Empty, String::Empty);
// the iCalendar text becomes a text/calendar alternate view of the message
SharedPtr<AlternateView> view = AlternateView::CreateAlternateViewFromString(ical, MakeObject<ContentType>(u"text/calendar"));
message->get_AlternateViews()->Add(view);
SharedPtr<MapiMessage> msg = MapiMessage::FromMailMessage(message);
msg->Save(u"DraftFromText.msg");
Tùy Chỉnh Một Cuộc Hẹn
Đặt Trạng Thái Tham Gia của Người Tham Dự
Khi một cuộc hẹn được ghi lại lại dưới dạng trả lời, mỗi người tham dự có thể mang một PARTSTAT giá trị. Gán một ParticipationStatus đến MailAddress đối tượng trước khi chúng được thêm vào bộ sưu tập người tham dự; giá trị xuất hiện như một tham số của ATTENDEE dòng.
DateTime startDate(2025, 12, 10, 10, 12, 11);
DateTime endDate(2025, 12, 13, 13, 11, 12);
SharedPtr<MailAddress> organizer = MakeObject<MailAddress>(u"aaa@amail.com", String(u"Organizer"));
SharedPtr<MailAddress> attendee1 = MakeObject<MailAddress>(u"bbb@bmail.com", String(u"First attendee"));
SharedPtr<MailAddress> attendee2 = MakeObject<MailAddress>(u"ccc@cmail.com", String(u"Second attendee"));
// the status ends up in the PARTSTAT parameter of each ATTENDEE line
attendee1->set_ParticipationStatus(ParticipationStatus::Accepted);
attendee2->set_ParticipationStatus(ParticipationStatus::Declined);
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(attendee1);
attendees->Add(attendee2);
SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"Room 12", startDate, endDate, organizer, attendees);
appointment->Save(u"WithParticipationStatus.ics", MakeObject<AppointmentIcsSaveOptions>());
Đặt Định Danh Sản Phẩm
Mỗi tệp iCalendar ghi tên phần mềm tạo ra nó trong PRODID thuộc tính. Để công bố định danh của riêng bạn, đặt ProductId trên các tùy chọn lưu - tĩnh Mặc Định instance là một điểm khởi đầu thuận tiện.
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"second@test.com"));
SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"location", u"test appointment", u"Test Description",
DateTime::get_Today(), DateTime::get_Today().AddDays(1), MakeObject<MailAddress>(u"first@test.com"), attendees);
// the value is written to the PRODID property of the calendar
SharedPtr<AppointmentIcsSaveOptions> saveOptions = AppointmentIcsSaveOptions::get_Default();
saveOptions->set_ProductId(u"Test Corporation");
appointment->Save(u"CustomProdId.ics", saveOptions);
Tải một Cuộc Hẹn
Phương thức tĩnh Load phương pháp phân tích tệp ICS (cũng có một overload luồng) và trả về một Appointment các thuộc tính của nó phản ánh VEVENT.
SharedPtr<Appointment> loaded = Appointment::Load(u"MonthlyMeeting.ics");
Console::WriteLine(u"Summary: " + loaded->get_Summary());
Console::WriteLine(u"Location: " + loaded->get_Location());
Console::WriteLine(u"Description: " + loaded->get_Description());
Console::WriteLine(u"Start date: " + loaded->get_StartDate().ToString());
Console::WriteLine(u"End date: " + loaded->get_EndDate().ToString());
Console::WriteLine(u"Organizer: " + loaded->get_Organizer()->ToString());
Console::WriteLine(u"Attendees: " + loaded->get_Attendees()->ToString());
Chuyển Đổi ICS sang EML hoặc MSG
ToMailMessage đóng gói cuộc hẹn thành một tin nhắn MIME với chế độ xem thay thế lịch, và ToMapiMessage sinh ra một mục Outlook. Cả hai kết quả đều có thể được lưu bằng các phương pháp thông thường.
SharedPtr<Appointment> appointment = Appointment::Load(u"MonthlyMeeting.ics");
// MIME message carrying the invitation
SharedPtr<MailMessage> eml = appointment->ToMailMessage();
eml->Save(u"MonthlyMeeting.eml", SaveOptions::get_DefaultEml());
// Outlook MAPI message
SharedPtr<MapiMessage> msg = appointment->ToMapiMessage();
msg->Save(u"MonthlyMeetingFromIcs.msg");
Kiểm Tra Phiên Bản iCalendar
Version trả về VERSION thuộc tính của tệp đã tải - "2.0" cho iCalendar (RFC 5545), "1.0" cho định dạng vCalendar cũ hơn - vì vậy một ứng dụng có thể phân nhánh dựa trên phương ngữ mà nó nhận được.
SharedPtr<Appointment> appointment = Appointment::Load(u"MonthlyMeeting.ics");
// Version holds the ICS/VCS version string, e.g. "2.0" for iCalendar
if (appointment->get_Version() == u"2.0")
{
Console::WriteLine(u"iCalendar 2.0 file");
}
Đọc Nhiều Sự Kiện từ Tệp ICS
Các xuất khẩu Lịch thường gộp nhiều sự kiện vào một tệp. CalendarReader lặp qua chúng: NextEvent tiến tới VEVENT tiếp theo và trả về false ở cuối, trong khi Hiện Tại tiết lộ sự kiện như một Appointment.
auto appointments = MakeObject<Collections::Generic::List<SharedPtr<Appointment>>>();
SharedPtr<CalendarReader> reader = MakeObject<CalendarReader>(u"US-Holidays.ics");
// each call moves to the next VEVENT; Current exposes it as an Appointment
while (reader->NextEvent())
{
appointments->Add(reader->get_Current());
}
Console::WriteLine(u"Events loaded: " + Convert::ToString(appointments->get_Count()));
for (SharedPtr<Appointment> appointment : appointments)
{
Console::WriteLine(appointment->get_StartDate().ToString(u"yyyy-MM-dd") + u" " + appointment->get_Summary());
}
Viết Nhiều Sự Kiện vào Tệp ICS
CalendarWriter là phần đối ứng cho đầu ra: mỗi Ghi gọi thêm một VEVENT vào cùng một VCALENDAR. Các Hành Động của các tùy chọn lưu quyết định METHOD được ghi vào tệp. Giải phóng writer để đóng lịch đúng cách.
SharedPtr<AppointmentIcsSaveOptions> saveOptions = MakeObject<AppointmentIcsSaveOptions>();
saveOptions->set_Action(AppointmentAction::Create);
SharedPtr<CalendarWriter> writer = MakeObject<CalendarWriter>(u"MultipleEvents.ics", saveOptions);
for (int32_t i = 0; i < 10; i++)
{
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"receiver@domain.com"));
SharedPtr<Appointment> appointment = MakeObject<Appointment>(String::Empty,
DateTime::get_Now().AddDays(i), DateTime::get_Now().AddDays(i).AddHours(1),
MakeObject<MailAddress>(u"sender@domain.com"), attendees);
appointment->set_Description(u"Test body " + Convert::ToString(i));
appointment->set_Summary(u"Test summary:" + Convert::ToString(i));
// every call appends one VEVENT to the same VCALENDAR
writer->Write(appointment);
}
writer->Dispose();
Tạo một Cuộc Hẹn Lặp Lại
Một chuỗi các cuộc họp là một cuộc hẹn duy nhất có mẫu lặp lại gắn vào Recurrence thuộc tính. Các lớp mẫu - DailyRecurrencePattern, WeeklyRecurrencePattern, MonthlyRecurrencePattern, YearlyRecurrencePattern - được xây dựng bằng số lần xuất hiện hoặc bằng ngày kết thúc. Gán một UniqueId là thực hành tốt: cùng một định danh được yêu cầu sau này để phát hành cập nhật hoặc hủy bỏ cho chuỗi. Ví dụ tạo 14 cuộc họp diễn ra vào Thứ Hai, Thứ Ba và Thứ Năm của mỗi tuần và lưu chuỗi dưới dạng ICS.
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"to@domain.com"));
DateTime startDate(2025, 12, 1, 17, 0, 0);
DateTime endDate(2025, 12, 1, 17, 30, 0);
SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"same place", startDate, endDate,
MakeObject<MailAddress>(u"from@domain.com"), attendees);
// keep the identifier so the series can be updated or cancelled later
appointment->set_UniqueId(Guid::NewGuid().ToString());
appointment->set_Description(u"Weekly sync");
// 14 occurrences on Monday, Tuesday and Thursday of every week
SharedPtr<WeeklyRecurrencePattern> pattern = MakeObject<WeeklyRecurrencePattern>(14);
pattern->set_StartDays(MakeArray<CalendarDay>({ CalendarDay::Monday, CalendarDay::Tuesday, CalendarDay::Thursday }));
pattern->set_Interval(1);
appointment->set_Recurrence(pattern);
appointment->Save(u"RecurringMeeting.ics", MakeObject<AppointmentIcsSaveOptions>());
Tệp kết quả chứa RRULE:FREQ=WEEKLY;WKST=MO;INTERVAL=1;COUNT=14;BYDAY=MO,TU,TH. Các chế độ xem thay thế được trả về bởi UpdateAppointment và CancelAppointment được xây dựng theo cùng một cách như RequestApointment và có thể được đính kèm vào một tin nhắn sau đó được gửi bằng một trong các máy khách email.
Làm việc với Các Mẫu Lặp Lại iCalendar
Trong thuật ngữ iCalendar, một mẫu lặp lại là mô tả ngắn gọn về một lịch trình từ đó các ngày riêng lẻ - các lần xuất hiện - có thể được tính toán. RFC 5545 diễn đạt nó bằng một vài thuộc tính: DTSTART (lần xuất hiện đầu tiên và điểm neo của mẫu), RRULE (luật lặp lại), RDATE (các ngày bổ sung để bao gồm), EXRULE (một quy tắc mô tả các ngày cần loại trừ) và EXDATE (các ngày riêng lẻ để loại trừ). Chỉ DTSTART là bắt buộc.
Cái Aspose::Email::Calendar::Recurrences namespace mô hình trực tiếp:
- CalendarRecurrence là toàn bộ mẫu. Nó giữ ngày bắt đầu và RRules, ExRules, RDates và ExDates bộ sưu tập, và nó có thể được tạo từ văn bản iCalendar hoặc được lắp ráp trong mã.
- RecurrenceRule là một
RRULEhoặcEXRULE. Các thuộc tính của nó (Frequency, Interval, Count, Until, ByDay, ByMonthDay, ByMonth, BySetPos và các phần khác) tương ứng một-một với các phần của quy tắc tiêu chuẩn.
Phân Tích Mẫu và Tạo Các Lần Xảy Ra
Cái CalendarRecurrence(String) hàm khởi tạo chấp nhận văn bản iCalendar; GenerateOccurrences mở rộng nó thành một DateCollection. Mười cuộc họp nhóm, mỗi thứ Hai lúc 10:00:
// Ten team meetings, every Monday at 10am
SharedPtr<CalendarRecurrence> pattern = MakeObject<CalendarRecurrence>(
u"DTSTART:20040301T100000\nRRULE:FREQ=WEEKLY;COUNT=10;BYDAY=MO");
SharedPtr<DateCollection> dates = pattern->GenerateOccurrences();
for (DateTime date : dates)
{
Console::WriteLine(date.ToString());
}
Xây Dựng Mẫu Trong Mã
Kết quả tương tự có thể đạt được mà không cần văn bản: đặt ngày bắt đầu, thêm một quy tắc vào RRules và điền vào các thuộc tính của nó. Các GenerateOccurrences(int32_t) quá tải giới hạn số ngày được tạo, đây là cách yêu cầu "N lần tiếp theo" của một quy tắc không giới hạn.
SharedPtr<CalendarRecurrence> recurrencePattern = MakeObject<CalendarRecurrence>();
recurrencePattern->set_StartDate(DateTime(1997, 9, 10, 9, 0, 0));
// build the RRULE part programmatically
SharedPtr<RecurrenceRule> rule = recurrencePattern->get_RRules()->Add();
rule->set_Frequency(Frequency::Monthly);
rule->set_Count(20);
rule->set_Interval(18);
rule->get_ByMonthDay()->Add(MakeArray<int32_t>({ 10, 11, 12, 13, 14, 15 }));
SharedPtr<DateCollection> expectedDates = recurrencePattern->GenerateOccurrences(20);
Console::WriteLine(u"expectedDates.Count = " + Convert::ToString(expectedDates->get_Count()));
for (DateTime date : expectedDates)
{
Console::WriteLine(u"DateTime = " + date.ToString());
}
Mô Tả Quy Tắc Bằng Ngôn Ngữ Đơn Giản
FriendlyText chuyển một quy tắc thành câu phù hợp cho giao diện người dùng. Quy tắc dưới đây tạo ra "Lặp lại mỗi tháng vào ngày 1 và ngày 1 từ cuối tháng với tối đa 2 lần lặp lại."
SharedPtr<RecurrenceRule> rule = MakeObject<RecurrenceRule>();
rule->set_Frequency(Frequency::Monthly);
rule->set_Count(2);
rule->get_ByMonthDay()->Add(1);
rule->get_ByMonthDay()->Add(-1);
Console::WriteLine(rule->get_FriendlyText());
Sổ Tay Quy Tắc
| Lịch Trình | RRULE | | — | — | | Ngày cuối cùng của mỗi tháng (-2 sẽ là ngày trước; 31 bỏ qua các tháng ngắn hơn) | FREQ=MONTHLY;BYMONTHDAY=-1 | | Ngày làm việc cuối cùng của mỗi tháng (BYSETPOS chọn ngày cuối cùng trong các ngày trong tuần được liệt kê) | FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1 | | Thứ Hai Cuối Năm | FREQ=YEARLY;BYDAY=-1MO | | Thứ Sáu của tuần ISO 8601 đầu năm | FREQ=YEARLY;BYWEEKNO=1;BYDAY=FR | | Thứ Sáu Đầu Năm | FREQ=YEARLY;BYDAY=1FR |
Giá Trị Ngày và Giờ trong Mẫu
- Một
DATEcó dạngyyyyMMdd(19970714). - Một thời gian địa phương
DATE-TIME(DTSTART:19980118T230000) là "floating": nó biểu thị cùng một thời gian đồng hồ tường trong bất kỳ múi giờ nào đang hiện hành. - Một UTC
DATE-TIMEcó một dấu cuốiZ(DTSTART:19980119T070000Z). Aspose.Email bỏ quaZvà coi giá trị là thời gian địa phương; trongUNTIL, nơi RFC 5545 yêu cầu UTC, bất kỳ dạng nào cũng được chấp nhận. - Một
DATE-TIMEđược chuẩn hóa vớiTZID(DTSTART;TZID=US-Eastern:19980119T020000) cũng được xem là thời gian địa phương; cácTZIDtham số hiện đang bị bỏ qua. BYWEEKNOtheo ISO 8601: tuần 1 là tuần đầu tiên có ít nhất bốn ngày trong năm mới. Nó chỉ hợp lệ khi cóFREQ=YEARLY, và các giá trị của nó nằm trong khoảng từ 1 đến 53 và từ -1 đến -53.
Xem Thêm
- Làm việc với các mục Lịch Outlook -
MapiCalendarcác mục ở định dạng MSG. - Làm việc với mục Lịch trong tệp PST - các mục lịch được lưu trong PST.