アポイントメントの管理: 作成と操作、ICS から MSG への変換

アポイントメントを作成し、MSG または ICS 形式でディスクに保存

この Appointment Aspose.Email for .NET のクラスを使用して新しいアポイントメントを作成できます。本記事では、まずアポイントメントを作成し、ICS 形式でディスクに保存します。アポイントメントを作成しディスクに保存するには以下の手順が必要です。

  1. インスタンスを作成します Appointment クラスをこのコンストラクタで初期化します。
  2. 上記コンストラクタに次の引数を渡します
    1. 場所
    2. 概要
    3. 説明
    4. 開始日
    5. 終了日
    6. 主催者
    7. 参加者
  3. 呼び出す Save() メソッドで、引数としてファイル名と形式を指定します。

この予定は、Microsoft Outlook またはICSファイルを読み込める任意のプログラムで開くことができます。ファイルを Microsoft Outlook で開くと、予定は自動的に Outlook カレンダーに追加されます。

以下のコードスニペットは、ICS または MSG 形式でアポイントメントをディスクに作成および保存する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET

// 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 説明を持つアポイントメントを作成するための以下のコードサンプルを試してください:

  1. Appointment クラスの新しいインスタンスを作成します。
  2. Appointment コンストラクタに必要なパラメーターを提供します:
    • アポイントメントの場所を指定します。
    • 開始日時を設定します。
    • 終了日時を設定します。
    • 主催者を指定します。
    • 出席者を指定します。
  3. 設定します HtmlDescription appointment オブジェクトのプロパティで、説明が HTML 形式であることを示します。
  4. appointment オブジェクトの Description プロパティを、複数行文字列で囲んだ HTML 形式の文字列に設定します:
    • HTML マークアップには、フォントスタイルを持つ "text" という名前の CSS クラスを定義する <style> ブロックが含まれています。
    • HTML 本文には CSS クラス "text" を持つ <p> タグと実際の招待メッセージが含まれています。
  5. アポイントメントオブジェクトの準備が完了しました。これでさらに操作を行うか、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");

IcsSaveOptions saveOptions = IcsSaveOptions.Default;
saveOptions.ProductId = "Test Corporation";
app.Save(dataDir + "ChangeProdIdOfICS.ics", saveOptions);

アポイントメントのロード

また、 Appointment このクラスは、ICS ファイルからアポイントメントをロードするために使用できます。

ICS形式で予定を読み込む

ICS形式で予定を読み込むには、次の手順が必要です:

  1. インスタンスを作成します Appointment クラス。
  2. 呼び出す Load() メソッドは ICS ファイルのパスを指定します。
  3. アポイントメント (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: " + appointment.Organizer);
Console.WriteLine("Attendees: " + appointment.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 ファイルに複数のイベントを書き込む

IcsSaveOptions saveOptions = new IcsSaveOptions();
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");

if (app.Version == 1.0)
{
    // do something
}