إدارة المواعيد: إنشاء، تحميل، تحويل ICS والعمل مع التكرارات
دعوات الاجتماعات، تصديرات التقويم وبيانات الجدولة كلها تُنقل كملفات iCalendar (ICS). في Aspose.Email للـ C++ يُمثَّل VEVENT واحد بـ Appointment فئة الـ Aspose::Email::Calendar المجال؛ قواعد التكرار تعيش في Aspose::Email::Calendar::Recurrences. يبقى هذا المقال على الجانب القائم على المعايير من التقويم. Outlook-المحددة MapiCalendar العناصر مشمولة في العمل مع عناصر تقويم Outlook، وعناصر التقويم المخزنة في PST في العمل مع عناصر التقويم في ملف PST.
العينات أدناه تستخدم الرؤوس والمساحات الاسمية التالية:
#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;
إنشاء موعد وحفظه كـ ICS أو MSG
المُنشئ ذو السبعة معاملات Appointment المُنشئ يأخذ الموقع، الملخص، الوصف، وقت البدء والانتهاء، المنظم كـ MailAddress والحضور كـ MailAddressCollection. حفظ يكتب النتيجة بالصِيغة المختارة بواسطة كائن الخيارات: AppointmentIcsSaveOptions لملف ICS الذي تستورده أي تطبيق تقويم، أو AppointmentMsgSaveOptions لبند Outlook MSG.
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>());
إضافة وصف HTML
iCalendar يسمح بتمثيل بديل، أكثر غنى للوصف عبر X-ALT-DESC خاصية. عيّن العلامة إلى HtmlDescription; العملاء الذين يفهمون الخاصية يعرضون HTML، والباقون يعودون إلى النص العادي. DESCRIPTION. المُنشئ الأقصر ذو الخمسة معاملات المُستخدم هنا يحذف الملخص والوصف.
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>());
إنشاء مسودة طلب اجتماع
طلب اجتماع هو موعد مدمج في رسالة بريد كـ text/calendar عرض بديل. للاحتفاظ به كمسودة يمكن إكمالها لاحقًا - في Outlook على سبيل المثال - عيّن MethodType إلى AppointmentMethodType::Publish، أرفق العرض الذي أنتجه RequestApointment إلى MailMessage، حوّل الرسالة إلى MAPI واحفظها كملف 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");
إنشاء مسودة من نص iCalendar
إذا كان الدعوة موجودة بالفعل كنص iCalendar - تم استلامه من نظام آخر، أو تم إنشاؤه بواسطة قالب - يمكن غلفه في AlternateView مع CreateAlternateViewFromString و text/calendar ContentType، ثم يُخزن بنفس الطريقة.
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");
تخصيص موعد
تعيين حالة مشاركة الحضور
عند كتابة موعد ردًا، يمكن لكل حاضر أن يحمل PARTSTAT القيمة. عيّن ParticipationStatus إلى الـ MailAddress الكائنات قبل إضافتها إلى مجموعة الحضور؛ تظهر القيمة كمعامل للـ ATTENDEE السطر.
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>());
تعيين معرف المنتج
كل ملف iCalendar يُسمي البرنامج الذي أنشأه في PRODID خاصية. لنشر المعرف الخاص بك، عيّن ProductId على خيارات الحفظ - الـ static الافتراضي النسخة هي نقطة انطلاق مريحة.
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);
تحميل موعد
الثابت Load الطريقة تحلل ملف ICS (يوجد تحميل زائد للمجرى أيضًا) وتُعيد Appointment التي تعكس خصائصها 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());
تحويل ICS إلى EML أو MSG
ToMailMessage يُغلف الموعد في رسالة MIME مع عرض تقويم بديل، و ToMapiMessage ينتج عنصر Outlook. يمكن حفظ أي من النتيجتين بالطرق المعتادة.
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");
تحقق من نسخة iCalendar
Version تُعيد الـ VERSION خاصية الملف المُحمَّل - "2.0" لـ iCalendar (RFC 5545)، "1.0" لصيغة vCalendar القديمة - بحيث يمكن للتطبيق التفرع بناءً على اللهجة المستلمة.
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");
}
قراءة أحداث متعددة من ملف ICS
غالبًا ما تُجمع تصديرات التقويم العديد من الأحداث في ملف واحد. CalendarReader يتكرر عبرهم: NextEvent يتقدم إلى VEVENT التالي ويُعيد false في النهاية، بينما الحالي يكشف الحدث كـ 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());
}
اكتب عدة أحداث إلى ملف ICS
CalendarWriter هو المقابل للإخراج: كل الكتابة الاتصال يُضيف VEVENT واحدًا إلى نفس VCALENDAR. الـ الإجراء من خيارات الحفظ يحدد الـ METHOD تمت كتابته إلى الملف. حرر الكاتب لإغلاق التقويم بشكل صحيح.
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();
إنشاء موعد متكرر
سلسلة من الاجتماعات هي موعد واحد مع نمط تكرار مرفق بـ Recurrence خاصية. فئات الأنماط - DailyRecurrencePattern, WeeklyRecurrencePattern, MonthlyRecurrencePattern, YearlyRecurrencePattern - تُبنى إما بعدد الحدوثات أو بتاريخ انتهاء. تعيين UniqueId ممارسة جيدة: يُطلب نفس المعرّف لاحقًا لإصدار تحديث أو إلغاء للمتسلسلة. المثال يخلق 14 اجتماعًا تُعقد أيام الاثنين والثلاثاء والخميس من كل أسبوع ويحفظ السلسلة كملف 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>());
الملف الناتج يحتوي على RRULE:FREQ=WEEKLY;WKST=MO;INTERVAL=1;COUNT=14;BYDAY=MO,TU,TH. العروض البديلة التي تُرجعها UpdateAppointment و CancelAppointment يتم بناؤها بنفس الطريقة مثل RequestApointment ويمكن إرفاقها برسالة تُرسل بعد ذلك بأحد عملاء البريد.
العمل مع أنماط تكرار iCalendar
في مصطلحات iCalendar، نمط التكرار هو وصف مضغوط لجدول يمكن من خلاله حساب التواريخ الفردية - الحدوثات - . يعبر RFC 5545 عنه بمجموعة من الخصائص: DTSTART (الحدوث الأول والمرساة للنمط)، RRULE (قاعدة التكرار)، RDATE (تواريخ إضافية للإدراج), EXRULE (قاعدة تصف تواريخ الاستبعاد) و EXDATE (تواريخ فردية للاستبعاد). فقط DTSTART إلزامي.
الـ Aspose::Email::Calendar::Recurrences المجال يعرض هذا مباشرة:
- CalendarRecurrence هو النمط بأكمله. يحمل تاريخ البدء و RRules, ExRules, RDates و ExDates المجموعات، ويمكن إنشاؤه من نص iCalendar أو تجميعه في الشفرة.
- RecurrenceRule هو واحد
RRULEأوEXRULE. خصائصه (Frequency, Interval, Count, Until, ByDay, ByMonthDay, ByMonth, BySetPos وما إلى ذلك) تتطابق واحدًا لواحد مع أجزاء القاعدة في المعيار.
تحليل نمط وتوليد تواريخه
الـ CalendarRecurrence(String) المنشئ يقبل نص iCalendar; GenerateOccurrences يوسعها إلى DateCollection. عشر اجتماعات فريق، كل إثنين الساعة 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());
}
إنشاء نمط بالشفرة
يمكن الحصول على نفس النتيجة بدون نص: حدد تاريخ البدء، أضف قاعدة إلى RRules واملأ خصائصها. الـ GenerateOccurrences(int32_t) التحمل الزائد يحدّ عدد التواريخ المنتجة، وهو الطريقة لطلب "المرة 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());
}
وصف قاعدة بكلمات بسيطة
FriendlyText يحوّل القاعدة إلى جملة مناسبة لواجهة المستخدم. القاعدة أدناه تنتج "يتكرر كل شهر في اليوم الأول والأول من النهاية من أيام الشهر بحد أقصى حدوثين."
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());
كتاب قواعد
| الجدول | RRULE | | — | — | | آخر يوم من كل شهر (-2 سيكون اليوم السابق؛ 31 يتخطى الشهور الأقصر) | FREQ=MONTHLY;BYMONTHDAY=-1 | | آخر يوم عمل في كل شهر (BYSETPOS يختار الأخير من أيام الأسبوع المذكورة) | FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1 | | آخر اثنين من السنة | FREQ=YEARLY;BYDAY=-1MO | | الجمعة من أول أسبوع ISO 8601 من السنة | FREQ=YEARLY;BYWEEKNO=1;BYDAY=FR | | أول الجمعة من السنة | FREQ=YEARLY;BYDAY=1FR |
قيم التاريخ والوقت في الأنماط
- A
DATEالقيمة لها الشكلyyyyMMdd(19970714). - محلي
DATE-TIME(DTSTART:19980118T230000) هو "عائم": يدل على نفس الوقت على الساعة في أي منطقة زمنية سائدة. - UTC
DATE-TIMEيحمل لاحقةZ(DTSTART:19980119T070000Z). Aspose.Email يتجاهل الـZويُعامل القيمة كوقت محلي؛ فيUNTIL, حيث يتطلب RFC 5545 UTC، يُقبل أي من الصيغ. - A
DATE-TIMEمؤهل بـTZID(DTSTART;TZID=US-Eastern:19980119T020000) يُعامل أيضًا كوقت محلي؛ الـTZIDالمعامل حالياً مهمل. BYWEEKNOيتبع ISO 8601: الأسبوع 1 هو أول أسبوع يحتوي على أربعة أيام على الأقل في السنة الجديدة. وهو صالح فقط معFREQ=YEARLY, وقيمها تتراوح من 1 إلى 53 ومن -1 إلى -53.
انظر أيضًا
- العمل مع عناصر تقويم Outlook -
MapiCalendarعناصر بصيغة MSG. - العمل مع عناصر التقويم في ملف PST - عناصر تقويم مخزنة في PST.