کار با موارد تقویم Outlook

Outlook قرارهای ملاقات و جلسات را به عنوان آیتم‌های تقویم ذخیره می‌کند. Aspose.Email برای C++ این آیتم را با MapiCalendar کلاس پوشش داده شده‌اند: می‌توان آن را از صفر ساخت، با یادآورها و منطقهٔ زمانی مورد نظر، و یا به صورت فرمت MSG Outlook یا به عنوان یک فایل iCalendar (ICS) ذخیره کرد. برخی ویژگی‌های قالب ICS، مانند چندین هشدار مستقل یا فایل‌های توکار، از طریق استانداردهای پایه‌ای Appointment کلاس به جای آن؛ هر دو در زیر نشان داده شده‌اند. آیتم‌های تقویمی که داخل یک فایل PST نگهداری می‌شوند در کار با آیتم‌های تقویم در فایل PST.

نمونه‌های این مقاله از این سرآیندها و فضای نام‌ها استفاده می‌کنند:

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <Calendar/Appointment.h>
#include <Calendar/AppointmentSaveFormat.h>
#include <Calendar/AppointmentReminder.h>
#include <Calendar/AppointmentReminderCollection.h>
#include <Calendar/CalendarReader.h>
#include <Calendar/ReminderAction.h>
#include <Calendar/ReminderAttachment.h>
#include <Calendar/ReminderAttachmentCollection.h>
#include <Calendar/ReminderAttendee.h>
#include <Calendar/ReminderAttendeeCollection.h>
#include <Calendar/ReminderDuration.h>
#include <Calendar/ReminderRelated.h>
#include <Calendar/ReminderTrigger.h>
#include <Mapi/MapiCalendar.h>
#include <Mapi/MapiCalendarIcsSaveOptions.h>
#include <Mapi/MapiCalendarState.h>
#include <Mapi/MapiCalendarTimeZone.h>
#include <Mapi/MapiElectronicAddress.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiConversionOptions.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiRecipientTrackStatus.h>
#include <Mapi/OutlookMessageFormat.h>
#include <Mapi/BodyContentType.h>
#include <system/collections/list_ext.h>
#include <system/convert.h>
#include <system/object_ext.h>
#include <system/timezone_info.h>
#include <system/timespan.h>
#include <system/uri.h>
#include <system/io/file.h>
#include <system/io/memory_stream.h>
#include <system/io/path.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Calendar;

ایجاد و ذخیره یک آیتم تقویم

ساده‌ترین سازندهٔ MapiCalendar محل، موضوع، بدنه و زمان شروع و پایان رویداد را می‌گیرد. ذخیره آیتم را در قالب انتخاب شده توسط AppointmentSaveFormat: Ics یک فایل iCalendar تولید می‌کند که تقریباً هر برنامه تقویمی می‌تواند آن را وارد کند، Msg یک مورد Outlook.

// location, subject, body, start and end
SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"LAKE ARGYLE WA 6743", u"Appointment",
    u"This is a very important meeting", DateTime(2025, 10, 2, 13, 0, 0), DateTime(2025, 10, 2, 14, 0, 0));

// iCalendar, readable by almost any calendar program
calendar->Save(u"CalendarItem.ics", AppointmentSaveFormat::Ics);

// Outlook's own format
calendar->Save(u"CalendarItem.msg", AppointmentSaveFormat::Msg);

Console::WriteLine(u"Saved: " + calendar->get_Subject());

افزودن یادآورها

یک یادآور نمایشی

یک مورد Outlook دارای یک یادآور منفرد است. ReminderSet آن را روشن می‌کند و ReminderDelta تعیین می‌کند چند دقیقه قبل از شروع، فعال شود. در یک فایل ICS، یادآور تبدیل به یک VALARM بلوک؛ نمونه خطوط کلیدی خود را چاپ می‌کند - TRIGGER:-PT45M و ACTION:DISPLAY.

SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"Home", u"Dentist",
    u"Bring the insurance card", DateTime(2025, 10, 2, 9, 0, 0), DateTime(2025, 10, 2, 10, 0, 0));

calendar->set_ReminderSet(true);
calendar->set_ReminderDelta(45);   // minutes before the start of the event

calendar->Save(u"CalendarWithDisplayReminder.ics", AppointmentSaveFormat::Ics);

for (String line : IO::File::ReadAllLines(u"CalendarWithDisplayReminder.ics"))
{
    if (line.StartsWith(u"BEGIN:VALARM") || line.StartsWith(u"TRIGGER") || line.StartsWith(u"ACTION"))
    {
        Console::WriteLine(line);
    }
}

یک یادآور صوتی

ReminderFileParameter نام یک فایل صوتی برای پخش به جای پیش‌فرض را می‌دهد. سپس یادآور به صورت یک هشدار صوتی نوشته می‌شود: ACTION:AUDIO با یک ATTACH خطی که به فایل اشاره می‌کند.

SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"Home", u"Dentist",
    u"Bring the insurance card", DateTime(2025, 10, 2, 9, 0, 0), DateTime(2025, 10, 2, 10, 0, 0));

calendar->set_ReminderSet(true);
calendar->set_ReminderDelta(58);                         // minutes before the start of the event
calendar->set_ReminderFileParameter(u"Alarm01.wav");     // the sound to play instead of the default one

calendar->Save(u"CalendarWithAudioReminder.ics", AppointmentSaveFormat::Ics);

for (String line : IO::File::ReadAllLines(u"CalendarWithAudioReminder.ics"))
{
    if (line.StartsWith(u"TRIGGER") || line.StartsWith(u"ACTION") || line.StartsWith(u"ATTACH"))
    {
        Console::WriteLine(line);
    }
}

چندین یادآور در یک رویداد iCalendar

قالب iCalendar اجازه می‌دهد هر تعداد هشدار برای هر رویداد داشته باشید، که هر کدام دارای محرک و عملی خاص خود هستند. Appointment آنها را از طریق یادآورها مجموعه‌ای از AppointmentReminder اشیاء:

  • ReminderTrigger - یا یک لحظه ثابت یا یک ReminderDuration نسبت به شروع یا پایان رویداد؛
  • Repeat و Duration - چند بار دیگر هشدار اجرا شود و در چه فاصله‌ای؛
  • ReminderAction - Audio, Display, Email یا Procedure، با یک فایل صوتی، یک متن، یک ایمیل یا یک برنامه به‌عنوان بار مفید.

نمونه یک هشدار از هر نوع اضافه می‌کند؛ فایل ذخیره‌شده شامل چهار VALARM بلوک‌ها.

SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"bbb@bmail.com", String(u"First attendee")));

SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"Meeting Location: Room 5",
    DateTime(1997, 3, 18, 18, 30, 0), DateTime(1997, 3, 18, 19, 30, 0),
    MakeObject<MailAddress>(u"aaa@amail.com", String(u"Organizer")), attendees);

// a sound at a fixed moment, repeated 4 more times every 15 minutes
SharedPtr<AppointmentReminder> audio = MakeObject<AppointmentReminder>();
audio->set_Trigger(MakeObject<ReminderTrigger>(DateTime(1997, 3, 17, 13, 30, 0, DateTimeKind::Utc)));
audio->set_Repeat(4);
audio->set_Duration(MakeObject<ReminderDuration>(TimeSpan(0, 15, 0)));
audio->set_Action(ReminderAction::Audio);
audio->get_Attachments()->Add(MakeObject<ReminderAttachment>(MakeObject<Uri>(u"ftp://Host.com/pub/sounds/bell-01.aud")));
appointment->get_Reminders()->Add(audio);

// a message 30 minutes before the start, repeated 2 more times every 15 minutes
SharedPtr<AppointmentReminder> display = MakeObject<AppointmentReminder>();
display->set_Trigger(MakeObject<ReminderTrigger>(MakeObject<ReminderDuration>(TimeSpan(0, -30, 0)), ReminderRelated::Start));
display->set_Repeat(2);
display->set_Duration(MakeObject<ReminderDuration>(TimeSpan(0, 15, 0)));
display->set_Action(ReminderAction::Display);
display->set_Description(u"Breakfast meeting with executive team at 8:30 AM EST");
appointment->get_Reminders()->Add(display);

// an email two days before the start, with a subject, a body and a link to a document
SharedPtr<AppointmentReminder> email = MakeObject<AppointmentReminder>();
email->set_Trigger(MakeObject<ReminderTrigger>(MakeObject<ReminderDuration>(TimeSpan(-2, 0, 0, 0)), ReminderRelated::Start));
email->get_Attendees()->Add(MakeObject<ReminderAttendee>(u"john_doe@host.com"));
email->set_Action(ReminderAction::Email);
email->set_Summary(u"REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING");
email->set_Description(u"A draft agenda needs to be sent out to the attendees of the weekly managers meeting.");
email->get_Attachments()->Add(MakeObject<ReminderAttachment>(MakeObject<Uri>(u"http://Host.com/templates/agenda.doc")));
appointment->get_Reminders()->Add(email);

// a program started at a fixed moment, repeated 23 more times every hour
SharedPtr<AppointmentReminder> procedure = MakeObject<AppointmentReminder>();
procedure->set_Trigger(MakeObject<ReminderTrigger>(DateTime(1998, 1, 1, 5, 0, 0, DateTimeKind::Utc)));
procedure->set_Repeat(23);
procedure->set_Duration(MakeObject<ReminderDuration>(TimeSpan(1, 0, 0)));
procedure->set_Action(ReminderAction::Procedure);
procedure->get_Attachments()->Add(MakeObject<ReminderAttachment>(MakeObject<Uri>(u"ftp://Host.com/novo-procs/felizano.exe")));
appointment->get_Reminders()->Add(procedure);

appointment->Save(u"AppointmentWithReminders.ics", AppointmentSaveFormat::Ics);

int32_t alarms = 0;
for (String line : IO::File::ReadAllLines(u"AppointmentWithReminders.ics"))
{
    if (line == u"BEGIN:VALARM")
    {
        alarms++;
    }
    if (line.StartsWith(u"ACTION"))
    {
        Console::WriteLine(line);
    }
}
Console::WriteLine(u"VALARM blocks: " + Convert::ToString(alarms));

تنظیم منطقه زمانی یک رویداد

MapiCalendarTimeZone می‌تواند مستقیماً از یک TimeZoneInfo شیء - اینجا منطقه زمانی کامپیوتری که برنامه روی آن اجرا می‌شود. اختصاص یافته به StartDateTimeZone و EndDateTimeZone، خروجی ICS زمان‌ها را در آن منطقه زمانی نشان می‌دهد، برای مثال DTSTART;TZID=Europe/Moscow:20251002T160000.

// a MAPI time zone built from the time zone of this computer
SharedPtr<MapiCalendarTimeZone> timeZone = MakeObject<MapiCalendarTimeZone>(TimeZoneInfo::get_Local());

SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"Office", u"Planning",
    u"Quarter planning", DateTime(2025, 10, 2, 13, 0, 0), DateTime(2025, 10, 2, 14, 0, 0));
calendar->set_StartDateTimeZone(timeZone);
calendar->set_EndDateTimeZone(timeZone);

calendar->Save(u"CalendarWithTimeZone.ics", AppointmentSaveFormat::Ics);

for (String line : IO::File::ReadAllLines(u"CalendarWithTimeZone.ics"))
{
    if (line.StartsWith(u"TZID") || line.StartsWith(u"DTSTART") || line.StartsWith(u"BEGIN:VTIMEZONE"))
    {
        Console::WriteLine(line);
    }
}

افزودن و خواندن پیوست‌ها

فایل‌های پیوست‌شده به یک Appointment از طریق پیوست‌ها مجموعه‌ها در فایل ICS تعبیه می‌شوند، بنابراین همراه آن سفر می‌کنند و هنگام باز شدن فایل با Appointment::Load.

SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"attendee@domain.com"));

SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"Home",
    DateTime(2025, 10, 2, 9, 0, 0), DateTime(2025, 10, 2, 10, 0, 0),
    MakeObject<MailAddress>(u"organizer@domain.com"), attendees);

// attach files from disk
for (String file : { String(u"1.jpg"), String(u"portrait.jpg") })
{
    appointment->get_Attachments()->Add(MakeObject<Attachment>(
        MakeObject<IO::MemoryStream>(IO::File::ReadAllBytes(file)), IO::Path::GetFileName(file)));
}
appointment->Save(u"AppointmentWithAttachments.ics", AppointmentSaveFormat::Ics);

// the attachments are embedded in the ICS file and come back when it is loaded
SharedPtr<Appointment> loaded = Appointment::Load(u"AppointmentWithAttachments.ics");
Console::WriteLine(u"Attachments: " + Convert::ToString(loaded->get_Attachments()->get_Count()));

for (SharedPtr<Attachment> attachment : loaded->get_Attachments())
{
    Console::WriteLine(u"   " + attachment->get_Name());
}

بررسی وضعیت پاسخ شرکت‌کنندگان جلسه

هر دریافت‌کنندهٔ درخواست جلسه‌ای ثبت می‌کند که آن شخص چه پاسخی داده است. MapiRecipient::get_RecipientTrackStatus به‌عنوان یک MapiRecipientTrackStatus مقدار - None تا زمانی که هنوز پاسخی نیست، سپس Tentative, Accepted یا Declined.

SharedPtr<MapiMessage> message = MapiMessage::Load(u"Test_Meeting.msg");

for (SharedPtr<MapiRecipient> recipient : message->get_Recipients())
{
    // None, Tentative, Accepted or Declined
    Console::WriteLine(recipient->get_DisplayName() + u": " + ObjectExt::ToString(recipient->get_RecipientTrackStatus()));
}

کنترل خروجی ICS یک مورد تقویم

MapiCalendarIcsSaveOptions شیء گزینه‌ها برای قالب ICS است. KeepOriginalDateTimeStamp می‌نویسد DTSTAMP مورد منبع به‌جای زمان جاری، و ProductIdentifier تنظیم می‌کند PRODID خطی که نام برنامهٔ تولیدکنندهٔ فایل را می‌نویسد.

SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"LAKE ARGYLE WA 6743", u"Appointment",
    u"This is a very important meeting", DateTime(2025, 10, 2, 13, 0, 0), DateTime(2025, 10, 2, 14, 0, 0));

SharedPtr<MapiCalendarIcsSaveOptions> icsSaveOptions = MakeObject<MapiCalendarIcsSaveOptions>();
icsSaveOptions->set_KeepOriginalDateTimeStamp(true);   // do not refresh DTSTAMP
icsSaveOptions->set_ProductIdentifier(u"Foo Ltd");     // the PRODID of the output file

calendar->Save(u"CalendarWithProductId.ics", icsSaveOptions);

Console::WriteLine(IO::File::ReadAllText(u"CalendarWithProductId.ics").Substring(0, 120));

بازرسی یک فایل تقویم قبل از خواندن آن

CalendarReader فایل را توصیف می‌کند که پیش از خواندن یک رویداد منفرد باز شده بود: Count تعداد VEVENT اجزا، IsMultiEvents می‌گوید آیا بیش از یک مورد وجود دارد، Version نسخه iCalendar و Method the METHOD خاصیت - PUBLISH, REQUEST, CANCEL و غیره. LoadAsMultiple همهٔ رویدادها را یک‌بار بازمی‌گرداند به‌جای قدم‌به‌قدم عبور از آن‌ها.

SharedPtr<CalendarReader> reader = MakeObject<CalendarReader>(u"US-Holidays.ics");

Console::WriteLine(u"Events in the calendar: " + Convert::ToString(reader->get_Count()));
Console::WriteLine(u"iCalendar version: " + reader->get_Version());
Console::WriteLine(u"Method: " + Convert::ToString(static_cast<int32_t>(reader->get_Method())));
Console::WriteLine(u"More than one event: " + Convert::ToString(reader->get_IsMultiEvents()));

// all the events at once instead of one by one
auto appointments = reader->LoadAsMultiple();
Console::WriteLine(u"Loaded: " + Convert::ToString(appointments->get_Count()));

حفظ بدنهٔ HTML و تنظیم صریح وضعیت

دو جزئیات هنگام تبدیل یک قرار به یک مورد Outlook مهم هستند.

به‌صورت پیش‌فرض بدنه به RTF تبدیل می‌شود، چون Outlook برای موارد تقویم این‌گونه ذخیره می‌کند. تنظیم ForcedRtfBodyForAppointment به false بدنهٔ HTML را حفظ می‌کند.

وضعیت یک مورد تقویم معمولاً چرخهٔ حیات خود را دنبال می‌کند: یک مورد که تازه ایجاد شده یک Meeting، و تبدیل می‌شود به Received وقتی به صندوق‌پست دریافت‌کننده می‌رسد. SetStateForced مقدار را مستقیماً اختصاص می‌دهد، که روش نگهداشتن اطلاعات سازمان‌دهنده هنگام ذخیره یک جلسه دریافت‌شده به‌صورت MSG است. ممکن است پس از آن مورد برای فوروارد یا ارسال مجدد مناسب نباشد.

// keep the HTML body of an appointment instead of converting it to RTF
SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"Test_Meeting.msg");

SharedPtr<MapiConversionOptions> conversionOptions = MakeObject<MapiConversionOptions>();
conversionOptions->set_Format(OutlookMessageFormat::Unicode);
conversionOptions->set_ForcedRtfBodyForAppointment(false); // the default is true

SharedPtr<MapiMessage> mapiMessage = MapiMessage::FromMailMessage(mailMessage, conversionOptions);
Console::WriteLine(u"Body type: " + ObjectExt::ToString(mapiMessage->get_BodyType()));

// set the state of a calendar item explicitly
SharedPtr<MapiCalendar> appointment = MakeObject<MapiCalendar>(u"LAKE ARGYLE WA 6743", u"Appointment",
    u"This is a very important meeting",
    DateTime(2025, 5, 10, 12, 30, 0, DateTimeKind::Utc),
    DateTime(2025, 5, 10, 13, 30, 0, DateTimeKind::Utc));

SharedPtr<MapiElectronicAddress> organizer = MakeObject<MapiElectronicAddress>();
organizer->set_EmailAddress(u"test@aaa.com");
organizer->set_DisplayName(u"test display Name");
appointment->set_Organizer(organizer);

// Meeting is the state of a new item; Received is what an item gets in the recipient's inbox
appointment->SetStateForced(MapiCalendarState::Meeting | MapiCalendarState::Received);

appointment->Save(u"ForcedState.msg", AppointmentSaveFormat::Msg);

خواندن و تنظیم شناسه محصول یک آیتم تقویم

یک فایل iCalendar محصولی که آن را ایجاد کرده در PRODID فیلد. Aspose.Email این شناسه را هنگام تبدیل یک آیتم تقویم بین ICS و MSG حفظ می‌کند، بنابراین می‌توانید منبع آیتم را ردیابی کنید. شناسه از طریق اعضای زیر در دسترس است:

  • Appointment::get_ProductId() و Appointment::set_ProductId() - دریافت یا تنظیم شناسه محصولی که یک Appointment.
  • MapiCalendar::get_ProductId() و MapiCalendar::set_ProductId() - دریافت یا تنظیم شناسه محصولی که یک MapiCalendar.
  • MapiCalendarSaveOptions::set_ProductIdentifier() - شناسه‌ای که هنگام ذخیره تقویم با MapiCalendarMsgSaveOptions یا MapiCalendarIcsSaveOptions.

کد نمونه زیر شناسهٔ محصول را از یک فایل ICS می‌خواند، قرار را به MSG تبدیل می‌کند با AppointmentMsgSaveOptions، آیتم تقویم را با یک شناسهٔ جدید ذخیره می‌کند و سپس شناسهٔ دیگری را در یک فایل ICS می‌نویسد:

#include <Calendar/Appointment.h>
#include <Calendar/AppointmentMsgSaveOptions.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiCalendar.h>
#include <Mapi/MapiCalendarMsgSaveOptions.h>
#include <Mapi/MapiCalendarIcsSaveOptions.h>
#include <Mapi/MapiItemType.h>
#include <system/io/memory_stream.h>
#include <system/console.h>

using namespace System;
using namespace Aspose::Email::Calendar;
using namespace Aspose::Email::Mapi;

// Read the product identifier from an iCalendar file
SharedPtr<Appointment> appointment = Appointment::Load(u"meeting.ics");
Console::WriteLine(u"Appointment product ID: " + appointment->get_ProductId());

// The identifier is kept when the appointment is converted to MSG
auto msgStream = MakeObject<IO::MemoryStream>();
appointment->Save(msgStream, MakeObject<AppointmentMsgSaveOptions>());
msgStream->set_Position(0);
SharedPtr<MapiMessage> msg = MapiMessage::Load(msgStream);

if (msg->get_SupportedType() == MapiItemType::Calendar)
{
    auto calendar = System::DynamicCast<MapiCalendar>(msg->ToMapiMessageItem());
    Console::WriteLine(u"MapiCalendar product ID: " + calendar->get_ProductId());

    // Save the calendar item to MSG with a different product identifier
    auto options = MakeObject<MapiCalendarMsgSaveOptions>();
    options->set_ProductIdentifier(u"-//Contoso//Calendar 2.0//EN");
    calendar->Save(u"meeting_out.msg", options);

    // The identifier set on the item is written to the PRODID field of an ICS file
    calendar->set_ProductId(u"-//Contoso//Calendar 3.0//EN");
    calendar->Save(u"meeting_out.ics", MakeObject<MapiCalendarIcsSaveOptions>());
}

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