مدیریت قرار ملاقات‌ها: ایجاد، بارگذاری، تبدیل به 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 را نشان می‌دهند، باقی‌ها به متن سادهfallback می‌شوند 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 در گزینه‌های ذخیره‌سازی - ثابت پیش‌فرض نمونه نقطهٔ شروعی مناسب است.

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 (یک overload برای Stream هم وجود دارد) را تجزیه می‌کند و یک 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 متقابلاً برای خروجی: هر Write فروش یک 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 یک روش خوب است: شناسایی‌کنندهٔ یکسان بعدها برای اعمال به‌روزرسانی یا لغو برای این سری ضروری است. این مثال ۱۴ جلسه در روزهای دوشنبه، سه‌شنبه و پنجشنبهٔ هر هفته ایجاد می‌کند و سری را به صورت 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(String) سازنده متن iCalendar را می‌پذیرد؛ GenerateOccurrences آن را به یک DateCollection. ده جلسهٔ تیم، هر دوشنبه ساعت ۱۰:۰۰:

// 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 و خصوصیات آن را تکمیل کنید. The 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 است: هفتهٔ ۱ اولین هفته‌ای است که حداقل چهار روز در سال جدید دارد. این صرفاً همراه با FREQ=YEARLY, و مقادیر آن در بازهٔ ۱ تا ۵۳ و از -۱ تا -۵۳ هستند.

همچنین ببینید