予定の管理:作成、読み込み、ICS 変換、繰り返し処理
会議招待、カレンダーエクスポート、スケジュールデータはすべて iCalendar (ICS) ファイルとしてやり取りされます。Aspose.Email for 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 として保存
7 引数の 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 は description の代替でリッチなレンダリングを提供します。 X-ALT-DESC プロパティ。マークアップを割り当てるのは HtmlDescription;このプロパティを理解するクライアントは HTML を表示し、他はプレーンテキストにフォールバックします。 DESCRIPTION。ここで使用されている 5 引数の短縮コンストラクタは、要約と説明を省略します。
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);
予定を読み込む
static 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 ファイルから複数のイベントを読み取る
カレンダーのエクスポートは、多くのイベントを 1 つのファイルにまとめることがよくあります。 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 は出力の対になるものです: すべての 書き込み 呼び出しは同じ VCALENDAR に VEVENT を 1 つ追加します。その アクション 保存オプションの設定が 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 の用語では、繰り返しパターンはスケジュールを簡潔に記述したもので、個々の日付(occurrences)を計算できます。RFC 5545 はそれをいくつかのプロパティで表現しています: DTSTART (パターンの最初の出現とアンカー), RRULE (繰り返しルール), RDATE (追加する日付) EXRULE (除外する日付を記述するルール)と EXDATE (除外する個別日付)。のみ DTSTART は必須です。
この Aspose::Email::Calendar::Recurrences 名前空間が直接モデル化します:
- CalendarRecurrence が全体のパターンです。開始日と RRules, ExRules, RDates および ExDates コレクションで、iCalendar テキストから作成することも、コードで組み立てることもできます。
- RecurrenceRule は 1 つの
RRULEまたはEXRULE。そのプロパティ(Frequency, Interval, Count, Until, ByDay, ByMonthDay, ByMonth, BySetPos (以下同様)は標準のルール部品と 1 対 1 対応します。
パターンを解析し、発生を生成
この CalendarRecurrence(String) コンストラクターは iCalendar テキストを受け取ります; GenerateOccurrences を展開して DateCollection例:10 回のチームミーティング、毎週月曜 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 そしてプロパティを入力します。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 ユーザーインターフェイス向けの文章に変換します。以下のルールは 「月の最初と最後から数えて 1 日目の間で最大 2 回繰り返す」 という文になります。
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パラメーターは現在無視されます。 BYWEEKNOISO 8601 に従い、週 1 は新年に少なくとも 4 日を含む最初の週です。これはFREQ=YEARLY、その値は 1 から 53、-1 から -53 の範囲です。
関連項目
- Outlook カレンダー アイテムの操作 -
MapiCalendarMSG 形式のアイテム。 - PST ファイルのカレンダー アイテムの操作 - PST に保存されたカレンダーアイテム。