管理约会:创建与操作,将 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" 内容类型包含纯文本描述,使用 "text/html" 内容类型包含 HTML 描述。每种备用表示都会添加 X-ALT-DESC 头部。要创建包含 HTML 内容的约会,请设置 HtmlDescription 属性。

尝试以下代码示例,以创建具有备用 HTML 描述的约会:

  1. 创建 Appointment 类的新实例。
  2. 向 Appointment 构造函数提供必要的参数:
    • 指定约会地点。
    • 设置开始日期和时间。
    • 设置结束日期和时间。
    • 指定组织者。
    • 指定与会者。
  3. 设置 HtmlDescription 约会对象的属性,表示描述是 HTML 格式。
  4. 将约会对象的 Description 属性设置为 HTML 格式的字符串,使用多行字符串括起来:
    • HTML 标记包含一个 <style> 块,定义了名为 "text" 的 CSS 类及其字体样式。
    • 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
}