アポイントメントの管理: 作成と操作、ICS から MSG への変換
この記事では、標準ベースの iCalendar (ICS) 予定を処理する方法を取り上げます。 Appointment クラス。Outlook と連携するには MapiCalendar アイテムについては、こちらをご覧ください Outlook カレンダー アイテムの管理; PST 内のカレンダー アイテムを保存および読み取る方法については、こちらをご覧ください PST ファイル内のカレンダー アイテムの管理.
アポイントメントを作成し、MSG または ICS 形式でディスクに保存
この Appointment Aspose.Email for .NET のクラスは新しい予定を作成するために使用できます。このガイドでは、まず予定を作成し、ICS 形式でディスクに保存します。予定を作成してディスクに保存するには、次の手順が必要です。
- インスタンスを作成します Appointment クラスをこのコンストラクタで初期化します。
- 上記コンストラクタに次の引数を渡します
- 場所
- 概要
- 説明
- 開始日
- 終了日
- 主催者
- 参加者
- 呼び出す Save() メソッドで、引数としてファイル名と形式を指定します。
この予定は、Microsoft Outlook またはICSファイルを読み込める任意のプログラムで開くことができます。ファイルを Microsoft Outlook で開くと、予定は自動的に Outlook カレンダーに追加されます。
以下のコードスニペットは、ICS または MSG 形式でアポイントメントをディスクに作成および保存する方法を示しています。
// Create and initialize an instance of the Appointment class
Appointment appointment = new Appointment(
"Meeting Room 3 at Office Headquarters",// Location
"Monthly Meeting", // Summary
"Please confirm your availability.", // Description
new DateTime(2015, 2, 8, 13, 0, 0), // Start date
new DateTime(2015, 2, 8, 14, 0, 0), // End date
"from@domain.com", // Organizer
"attendees@domain.com"); // Attendees
// Save the appointment to disk in ICS format
appointment.Save(fileName + ".ics", new AppointmentIcsSaveOptions());
Console.WriteLine("Appointment created and saved to disk successfully.");
// Save the appointment to disk in MSG format
appointment.Save(fileName + ".msg", new AppointmentMsgSaveOptions());
Console.WriteLine("Appointment created and saved to disk successfully.");
HTML コンテンツで予定を作成
X-ALT-DESC ヘッダーを使用して、イベントの説明を異なるコンテンツタイプで代替的に表現できます。これにより iCalendar ファイルの受信者は自分のニーズに最適な表現を選択できます。例えば、プレーンテキストの説明("text/plain")や HTML の説明("text/html")を含めることができます。代替表現ごとに X-ALT-DESC ヘッダーが追加されます。HTML コンテンツのアポイントメントを作成するには、次を設定します。 HtmlDescription プロパティです。
代替の HTML 説明を持つアポイントメントを作成するための以下のコードサンプルを試してください:
- Appointment クラスの新しいインスタンスを作成します。
- Appointment コンストラクタに必要なパラメーターを提供します:
- アポイントメントの場所を指定します。
- 開始日時を設定します。
- 終了日時を設定します。
- 主催者を指定します。
- 出席者を指定します。
- 設定します HtmlDescription appointment オブジェクトのプロパティで、説明が HTML 形式であることを示します。
- appointment オブジェクトの Description プロパティを、複数行文字列で囲んだ HTML 形式の文字列に設定します:
- HTML マークアップには、フォントスタイルを持つ "text" という名前の CSS クラスを定義する <style> ブロックが含まれています。
- HTML 本文には CSS クラス "text" を持つ <p> タグと実際の招待メッセージが含まれています。
- アポイントメントオブジェクトの準備が完了しました。これでさらに操作を行うか、iCalendar ファイルとして保存できます。
var appointment = new Appointment("Bygget 83",
DateTime.UtcNow, // start date
DateTime.UtcNow.AddHours(1), // end date
new MailAddress("TintinStrom@from.com", "Tintin Strom"), // organizer
new MailAddress("AinaMartensson@to.com", "Aina Martensson")) // attendee
{
HtmlDescription = @"
<html>
<style type=""text/css"">
.text {
font-family:'Comic Sans MS';
font-size:16px;
}
</style>
<body>
<p class=""text"">Hi, I'm happy to invite you to our party.</p>
</body>
</html>"
};
ドラフト予定リクエストを作成
以前の記事で、ICS 形式でアポイントメントを作成および保存する方法を示しました。基本情報を追加した後、ドラフトモードでアポイントメントリクエストを作成し、個々の使用状況に応じて必要な変更を加えるために他のユーザーに転送することがよく求められます。アポイントメントをドラフトモードで保存するには、 MethodType Appointment クラスのプロパティは次のように設定すべきです AppointmentMethodType.Publish以下のコードスニペットは、ドラフトのアポイントメントリクエストを作成する方法を示しています。
string sender = "test@gmail.com";
string recipient = "test@email.com";
MailMessage message = new MailMessage(sender, recipient, string.Empty, string.Empty);
Appointment app = new Appointment(string.Empty, DateTime.Now, DateTime.Now, sender, recipient)
{
MethodType = AppointmentMethodType.Publish
};
message.AddAlternateView(app.RequestApointment());
MapiMessage msg = MapiMessage.FromMailMessage(message);
// Save the appointment as draft.
msg.Save(dstDraft);
Console.WriteLine(Environment.NewLine + "Draft saved at " + dstDraft);
テキストからドラフト予定の作成
次のコードスニペットは、テキストからドラフト予定を作成する方法を示しています。
string ical = @"BEGIN:VCALENDAR
METHOD:PUBLISH
PRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN=test@gmail.com:mailto:test@gmail.com
DTSTART:20130220T171439
DTEND:20130220T174439
DTSTAMP:20130220T161439Z
END:VEVENT
END:VCALENDAR";
string sender = "test@gmail.com";
string recipient = "test@email.com";
MailMessage message = new MailMessage(sender, recipient, string.Empty, string.Empty);
AlternateView av = AlternateView.CreateAlternateViewFromString(ical, new ContentType("text/calendar"));
message.AlternateViews.Add(av);
MapiMessage msg = MapiMessage.FromMailMessage(message);
msg.Save(dataDir + "draft_out.msg");
アポイントメントのカスタマイズ
予定参加者のステータスを設定
Aspose.Email for .NET API は、返信メッセージを作成する際にアポイントメント参加者のステータスを設定できます。これにより、ICS ファイルに PARTSTAT プロパティが追加されます。
DateTime startDate = new DateTime(2011, 12, 10, 10, 12, 11),
endDate = new DateTime(2012, 11, 13, 13, 11, 12);
MailAddress organizer = new MailAddress("aaa@amail.com", "Organizer");
MailAddressCollection attendees = new MailAddressCollection();
MailAddress attendee1 = new MailAddress("bbb@bmail.com", "First attendee");
MailAddress attendee2 = new MailAddress("ccc@cmail.com", "Second attendee");
attendee1.ParticipationStatus = ParticipationStatus.Accepted;
attendee2.ParticipationStatus = ParticipationStatus.Declined;
attendees.Add(attendee1);
attendees.Add(attendee2);
Appointment target = new Appointment(location, startDate, endDate, organizer, attendees);
iCalendar の製品識別子をカスタマイズ
Aspose.Email for .NET API は、iCalendar オブジェクトを作成した製品識別子の取得または設定を可能にします。
string description = "Test Description";
Appointment app = new Appointment("location", "test appointment", description, DateTime.Today,
DateTime.Today.AddDays(1), "first@test.com", "second@test.com");
AppointmentIcsSaveOptions saveOptions = AppointmentIcsSaveOptions.Default;
saveOptions.ProductId = "Test Corporation";
app.Save(dataDir + "ChangeProdIdOfICS.ics", saveOptions);
アポイントメントのロード
また、 Appointment このクラスは、ICS ファイルからアポイントメントをロードするために使用できます。
ICS形式で予定を読み込む
ICS形式で予定を読み込むには、次の手順が必要です:
- インスタンスを作成します Appointment クラス。
- 呼び出す Load() メソッドは ICS ファイルのパスを指定します。
- アポイントメント (ICS ファイル) から任意のプロパティを読み取り、情報を取得します。
以下のコードスニペットは、ICS 形式のアポイントメントをロードする方法を示しています。
// Load an Appointment just created and saved to disk and display its details.
Appointment loadedAppointment = Appointment.Load(dstEmail);
Console.WriteLine(Environment.NewLine + "Loaded Appointment details are as follows:");
// Display the appointment information on screen
Console.WriteLine("Summary: " + loadedAppointment.Summary);
Console.WriteLine("Location: " + loadedAppointment.Location);
Console.WriteLine("Description: " + loadedAppointment.Description);
Console.WriteLine("Start date: " + loadedAppointment.StartDate);
Console.WriteLine("End date: " + loadedAppointment.EndDate);
Console.WriteLine("Organizer: " + loadedAppointment.Organizer);
Console.WriteLine("Attendees: " + loadedAppointment.Attendees);
Console.WriteLine(Environment.NewLine + "Appointment loaded successfully from " + dstEmail);
ICS を MSG に変換
API を使用すると、アポイントメントをメッセージオブジェクトに簡単に変換できます。以下のコード例は、アポイントメントリクエストを MailMessage または MapiMessage に変換する方法を示しています。
var appointment = Appointment.Load("appRequest.ics");
var eml = appointment.ToMailMessage();
var msg = appointment.ToMapiMessage();
ICS ファイルから複数のイベントを読み取る
List<Appointment> appointments = new List<Appointment>();
CalendarReader reader = new CalendarReader(dataDir + "US-Holidays.ics");
while (reader.NextEvent())
{
appointments.Add(reader.Current);
}
//working with appointments...
ICS ファイルに複数のイベントを書き込む
AppointmentIcsSaveOptions saveOptions = new AppointmentIcsSaveOptions();
saveOptions.Action = AppointmentAction.Create;
using (CalendarWriter writer = new CalendarWriter(dataDir + "WriteMultipleEventsToICS_out.ics", saveOptions))
{
for (int i = 0; i < 10; i++)
{
Appointment app = new Appointment(string.Empty, DateTime.Now, DateTime.Now, "sender@domain.com", "receiver@domain.com");
app.Description = "Test body " + i;
app.Summary = "Test summary:" + i;
writer.Write(app);
}
}
アポイントメントのバージョンを判定
アポイントメントのバージョンを判定するには、次を使用できます。 Appointment.Version プロパティ( Appointment クラス。このプロパティは、ファイルがどのバージョンに基づいているかを判定するのに役立ち、他のシステムやアプリとの統合を保証します。
以下のコードサンプルは、このプロパティをプロジェクトで実装する方法を示しています。
var app = Appointment.Load("meeting.ics");
// Version is a string that holds the ICS/VCS version, e.g. "2.0" for iCalendar
if (app.Version == "2.0")
{
// do something
}
会議リクエストの送信とキャンセル
この Appointment クラスに、 SmtpClientは、会議リクエスト、繰り返し会議リクエスト、更新、キャンセルをメールで送信するために使用できます。予定は MailMessage 代替ビューとして。
会議リクエストの送信
会議リクエストを送信するには、 Appointmentをメッセージに追加し、 RequestApointment メソッドでメッセージを送信します。予定の UniqueId 値を割り当てておくと、更新やキャンセルを送信する際に同じ予定を参照できます。
// Create an instance of SmtpClient
SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "user@gmail.com", "password");
client.SecurityOptions = SecurityOptions.Auto;
// Gather the attendees
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add(new MailAddress("first.attendee@domain.com", "First Attendee"));
attendees.Add(new MailAddress("second.attendee@domain.com", "Second Attendee"));
// Create the message and the appointment
MailMessage msg = new MailMessage();
msg.From = "organizer@domain.com";
msg.To = attendees;
Appointment app = new Appointment("Meeting Room 1", DateTime.Now, DateTime.Now.AddHours(1), msg.From, attendees);
app.Summary = "Monthly Meeting";
app.Description = "Please confirm your availability.";
// Add the appointment to the message and send it
msg.AddAlternateView(app.RequestApointment());
client.Send(msg);
繰り返し会議リクエストの送信
再発付きの会議リクエストを作成するには、再発パターン (例: ) を割り当てます。 WeeklyRecurrencePattern) を Appointment.Recurrence プロパティ。予定の一意 ID を保存しておくと、後で更新を送信できるようになります。
// Create a mail message
MailMessage msg1 = new MailMessage();
msg1.To.Add("to@domain.com");
msg1.From = new MailAddress("from@gmail.com");
// Fill the appointment object
DateTime startDate = new DateTime(2013, 12, 1, 17, 0, 0);
DateTime endDate = new DateTime(2013, 12, 31, 17, 30, 0);
Appointment agendaAppointment = new Appointment("same place", startDate, endDate, msg1.From, msg1.To);
// Create a unique id so the appointment can be accessed later
string szUniqueId = Guid.NewGuid().ToString();
agendaAppointment.UniqueId = szUniqueId;
agendaAppointment.Description = "----------------";
// Create a weekly recurrence pattern: Mon, Tue and Thu
WeeklyRecurrencePattern pattern1 = new WeeklyRecurrencePattern(14);
pattern1.StartDays = new CalendarDay[3];
pattern1.StartDays[0] = CalendarDay.Monday;
pattern1.StartDays[1] = CalendarDay.Tuesday;
pattern1.StartDays[2] = CalendarDay.Thursday;
pattern1.Interval = 1;
// Set the recurrence pattern for the appointment
agendaAppointment.Recurrence = pattern1;
// Attach the appointment to the mail
msg1.AlternateViews.Add(agendaAppointment.RequestApointment());
// Send the mail with the appointment request
SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "your.email@gmail.com", "your.password");
client.SecurityOptions = SecurityOptions.Auto;
client.Send(msg1);
予定更新リクエストの送信
以前に送信した予定の更新を送信するには、予定の一意の ID が必要です。次を使用してください: UpdateAppointment メソッドで更新用の代替ビューを構築します。
static public void SendUpdate(string szUniqueId)
{
DateTime startDate = new DateTime(2013, 12, 12, 17, 0, 0);
DateTime endDate = new DateTime(2013, 12, 12, 17, 30, 0);
Appointment appUpdate = new Appointment("Different Place", startDate, endDate,
"organizer@gmail.com", "attendee@domain.com");
appUpdate.UniqueId = szUniqueId;
appUpdate.Summary = "update meeting request summary";
appUpdate.Description = "update";
MailMessage msgUpdate = new MailMessage("organizer@gmail.com", "attendee@domain.com",
"test email - update meeting request", "test email");
msgUpdate.AddAlternateView(appUpdate.UpdateAppointment());
SmtpClient smtp = new SmtpClient("server.domain.com", 587, "username", "password");
smtp.Send(msgUpdate);
}
会議リクエストのキャンセル
会議をキャンセルするには、同じ Appointment (リクエスト送信時に保存した情報を使用して)、メッセージに CancelAppointment メソッドを使用し、出席者にメッセージを送信します。
// Re-create the attendee collection and the appointment from your stored data
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add(new MailAddress("first.attendee@domain.com", "First Attendee"));
attendees.Add(new MailAddress("second.attendee@domain.com", "Second Attendee"));
Appointment app = new Appointment("Meeting Room 1", "Monthly Meeting", "Please confirm your availability.",
DateTime.Now, DateTime.Now.AddHours(1),
new MailAddress("organizer@domain.com", "Organizer"), attendees);
// Create the cancellation message
MailMessage msg = new MailMessage();
msg.From = new MailAddress("organizer@domain.com", "");
msg.To = attendees;
msg.Subject = "Cancel meeting";
msg.AddAlternateView(app.CancelAppointment());
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587, "user@gmail.com", "password");
smtp.Send(msg);
iCalendar 再発パターンの操作
再発パターン は特定のスケジュールを記述する手段です。スケジュールに従って発生 (日付と時刻) のリストを生成するために必要な情報を含みます。再発パターンは、全体のパターンを構成するサイクルを記述する再発ルールを含むことがあります。一般に、再発パターンが複雑になるほど、含まれる再発ルールの数も増えます。
再発パターンは 例外 を含めることができます (実行時エラーとは異なる例外です)。例外は元のパターンに対して発生日を追加または除外します。明示的な発生として、またはパターン自体として指定できます。例外を伴う再発パターンの例:
- 6月から8月を除く、毎月第2金曜日。
- 1月を除く毎月の1日。ただし、1月は2日に設定すべきです。
iCalendar RFC は コンポーネント (例: VEVENT や VTODO) を定義し、イベントやタスクを表します。コンポーネントは開始日時、説明、場所、出席者、再発などのプロパティを持ちます。再発パターンは通常、繰り返しタスクやイベントのプロパティとして存在します。iCalendar が定義する再発パターンのプロパティは次のとおりです:
- DTSTART – パターンの開始日時 (明示的に除外されない限り最初の発生も表す)。
- RRULE – 再発セットの繰り返しルールを指定します。
- RDATE – 再発セットに含める日付と時刻の一覧を定義します。
- EXRULE – 再発セットから除外するルールを指定します。
- EXDATE – 再発セットから除外する日付と時刻の一覧を定義します。
DTSTART は必須で、1 つだけ指定できます。他のプロパティはオプションで、複数回指定可能です。
Aspose.Email 再発オブジェクトモデル
この Aspose.Email.Calendar.Recurrences 名前空間には iCalendar 再発を操作するためのクラスが含まれます。 CalendarRecurrence および RecurrenceRule は中心クラスで、対応する RFC 2445 要素の具体実装を提供します。
- この
CalendarRecurrenceクラスは全体の再発パターンを表します。デフォルトコンストラクタで新規に作成するか、静的メソッドで iCalendar 形式の既存パターンを読み込むことができます。FromiCalendarメソッド。 - この
RecurrenceRuleクラスは、再帰パターンの RRULE または EXRULE 部分を表します。RecurrenceRuleは iCalendar 標準の対応プロパティを直接公開します。例:ByMonthiCalendar の BYMONTH へマップされるなど。これらの値を調べたり設定したりすることで、RecurrenceRuleプロパティにより、再発ルールを解析または変更できます。
以下のコードスニペットは、再発パターン (RRULE 部分に再発ルールが含まれる) を読み込み、発生を生成します:
// Ten team meetings, every Monday at 10am.
CalendarRecurrence pattern = new CalendarRecurrence("DTSTART:20040301T100000\n" + "RRULE:FREQ=WEEKLY;COUNT=10;BYDAY=MO");
DateCollection dates = pattern.GenerateOccurrences();
再発パターンから発生を生成
Aspose.Email を使用すると、再発パターンから発生を生成できます。次の「次の」発生を取得するには、 GenerateOccurrences パラメーターで nNextOccurrences = 1。以下のコードスニペットは、 GenerateOccurrences(20).
CalendarRecurrence recurrencePattern = new CalendarRecurrence();
recurrencePattern.StartDate = new DateTime(1997, 9, 10, 9, 0, 0);
RecurrenceRule rule = recurrencePattern.RRules.Add();
rule.Frequency = Frequency.Monthly;
rule.Count = 20;
rule.Interval = 18;
rule.ByMonthDay.Add(new int[] { 10, 11, 12, 13, 14, 15 });
DateCollection expectedDates = recurrencePattern.GenerateOccurrences(20);
Console.WriteLine("expectedDates.Count = " + expectedDates.Count);
foreach (DateTime date in expectedDates)
{
Console.WriteLine("DateTime = " + date);
}
再発のユーザーフレンドリーテキストを取得
ルールのユーザーフレンドリーなテキストは、次を使用して取得できます: FriendlyText プロパティ。以下のコードの出力は: "月の 1 日と月末から数えて 1 日目に最大 2 回発生する" です。
RecurrenceRule rule = new RecurrenceRule();
rule.Frequency = Frequency.Monthly;
rule.Count = 2;
rule.ByMonthDay.Add(1);
rule.ByMonthDay.Add(-1);
Console.WriteLine(rule.FriendlyText);
サンプル再発パターン
以下のサンプル RRULE 文字列は、一般的なスケジュールの表現例です。
-
毎月の最終日。月の最終日の前日を取得したい場合は、次を使用します:
BYMONTHDAY=-2。次のように指定した場合BYMONTHDAY=31、したがって iCalendar 標準に従うと、31 日未満の月では発生は生成されません。RRULE:FREQ=MONTHLY;BYMONTHDAY=-1 -
毎月の最後の営業日。このルールは月内のすべての営業日を指定し、その中で最後のものを選びます。結果として月の最後の営業日が得られます。
RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1 -
年の最後の月曜日。
RRULE:FREQ=YEARLY;BYDAY=-1MO -
年の最初の ISO 8601 週の金曜日。ISO 8601 仕様では、年の最初の週は少なくとも 4 日が含まれる最初の週と定義されています。
FREQ=YEARLY;BYWEEKNO=1;BYDAY=FR -
年の最初の金曜日。たとえば 1999 年の場合、年の最初の金曜日は 1999/01/01 ですが、ISO 8601 の第 1 週の金曜日は 1999/01/08 です。
FREQ=YEARLY;BYDAY=1FR
重要な iCalendar (RFC 2445) の詳細
日付、または日時を伴う日付は、再発パターンを指定する際に DTSTART、UNTIL、EXDATE、RDATE 要素で使用できます。iCalendar は、カレンダー日付のみを含む DATE 型と、正確な日付と時刻を示す DATE-TIME 型を定義しています。DATE-TIME 値は、ローカル時間、UTC 時間、タイムゾーン付きローカル時間の 3 形態で指定できます。
- DATE。iCalendar 標準に従い、DATE 値は
yyyyMMdd形式です。例:199707141997 年 7 月 14 日を表します。 - ローカル時間を含む DATE-TIME。ローカル時間形式は、UTC 指示子を含まず、タイムゾーンを参照しない日時値です。例:
DTSTART:19980118T2300001998 年 1 月 18 日午後 11 時を表します。この形式の日時は「フローティング」と呼ばれ、特定のタイムゾーンに束縛されません。観測中のタイムゾーンに関係なく、同じ時・分・秒の値を表します。 - UTC 時間を含む DATE-TIME。UTC 時間 (絶対時間) の日付は大文字の文字で示されます。
Z時間値に付加された接尾辞の例です:DTSTART:19980119T070000Z1998 年 1 月 19 日 07:00 UTC を表します。Aspose.Email は UTC 接尾辞を無視します。Z接尾辞を付け、ローカル時間として扱います。RFC 2445 標準では、再発パターンの UNTIL ルールで指定された時間部分は UTC 形式である必要があるとしていますが、Aspose.Email は UNTIL ルールで任意の形式の時間を受け付けます。 - ローカル時間とタイムゾーンを含む DATE-TIME。タイムゾーンを参照するには、DATE-TIME に TZID プロパティを付加します。例:
DTSTART;TZID=US-Eastern:19980119T0200001998 年 1 月 19 日ニューヨーク時間の午前 2 時を表します。現在、Aspose.Email は TZID パラメータを無視し、ローカル時間として扱います。 - BYWEEKNO と ISO 8601 の準拠。BYWEEKNO は、 ISO 8601 が必要です。ISO 8601 で定義された週番号は、通常の感覚とは異なります。暦年の第 1 週は、少なくとも 4 日を含む最初の週です。BYWEEKNO ルールは、年内の週を識別する番号のコンマ区切りリストを指定します (有効な値は 1 から 53 と -1 から -53) 。このルールは YEARLY ルールでのみ有効です。
関連項目
- Outlook カレンダー アイテムの管理 — Outlook としてカレンダー アイテムを作成および管理する
MapiCalendar(MSG) オブジェクト。 - PST ファイル内のカレンダー アイテムの管理 — PST 内のカレンダー アイテムを保存および読み取ります。