管理 Outlook 日历项
本文介绍了 Outlook MapiCalendar 以 MSG 保存的日历项。要使用基于标准的 iCalendar(ICS)约会和会议请求,请参见 管理约会;要在 PST 中存储和读取日历项,请参见 在 PST 文件中管理日历项.
Aspose.Email MapiCalendar 类提供设置日历项各种属性的方法和属性。本节提供以下代码示例:
- 创建并保存日历项
- 将日历项保存为 MSG 文件
- 将 MAPI 日历项的产品 ID 保存到 ICS
- 获取事件总数
- 添加显示提醒
- 添加音频提醒
- 添加/检索日历文件附件
- 检查会议请求中收件人的状态
- 从标准时区创建 MAPI 日历时区
- 为约会设置提醒
- 将预约 EML 转换为带 HTML 正文的 MSG
- 手动设置 MAPI 日历项的状态
创建并保存日历项
以下代码片段演示了如何创建并以 ICS 格式保存日历项。
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Create the appointment
MapiCalendar calendar = new MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
new DateTime(2012, 10, 2, 13, 0, 0),
new DateTime(2012, 10, 2, 14, 0, 0));
calendar.Save(dataDir + "CalendarItem_out.ics", AppointmentSaveFormat.Ics);
将日历项保存为 MSG 文件
以下代码片段演示了如何将日历项保存为 MSG。
calendar.Save(dataDir + "CalendarItemAsMSG_out.Msg", AppointmentSaveFormat.Msg);
将 MAPI 日历项的产品 ID 保存到 ICS
该 ProductIdentifier 属性的 MapiCalendarIcsSaveOptions 该类用于将 MAPI 日历项保存为 iCalendar (ICS) 文件,保留原始的日期和时间信息以及自定义产品标识符。此属性指定创建 iCalendar 对象的产品标识符。
以下代码示例展示了如何在 MAPI 日历对象中处理 iCalendar (ICS) 数据:
var icsSaveOptions = new MapiCalendarIcsSaveOptions
{
KeepOriginalDateTimeStamp = true,
ProductIdentifier = "Foo Ltd"
};
mapiCalendar.Save("my.ics", icsSaveOptions);
获取事件总数
该 CalendarReader 类可轻松处理日历事件。以下属性和一个方法允许您处理多个事件:
- CalendarReader.Count - CalendarReader 类的 Count 属性允许您检索日历中存在的 Vevent 组件(事件)的数量,便于跟踪事件总数。
- CalendarReader.IsMultiEvents - 此属性用于判断日历是否包含多个事件。它返回布尔值,指示日历中是否存在多个事件,有助于识别单事件或多事件的日历。
- CalendarReader.Method - Method 属性获取与日历对象关联的 iCalendar 方法类型。它返回方法类型,例如 “REQUEST”、 “PUBLISH” 或 “CANCEL”,提供有关日历用途的有价值信息。
- CalendarReader.Version - 获取 iCalendar 的版本。
- CalendarReader.LoadAsMultiple() 此方法可从包含多个事件的日历中加载事件列表。它返回 Appointment 对象列表,便于单独访问和处理每个事件。
以下代码示例演示了如何在项目中实现这些功能:
var reader = new CalendarReader(fileName);
Console.WriteLine("Calendar contains " + reader.Count + " events");
Console.WriteLine("The Version of the calendar is " + reader.Version);
Console.WriteLine("The Method of the calendar is " + reader.Method);
Console.WriteLine("Does the calendar contain multiple events? - " + reader.IsMultiEvents);
List<Appointment> appointments = reader.LoadAsMultiple();
添加显示提醒
以下代码片段展示了如何向日历添加显示提醒。
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Create Appointment
Appointment app = new Appointment("Home", DateTime.Now.AddHours(1), DateTime.Now.AddHours(1), "organizer@domain.com", "attendee@gmail.com");
MailMessage msg = new MailMessage();
msg.AddAlternateView(app.RequestApointment());
MapiMessage mapi = MapiMessage.FromMailMessage(msg);
MapiCalendar calendar = (MapiCalendar)mapi.ToMapiMessageItem();
// Set calendar Properties
calendar.ReminderSet= true;
calendar.ReminderDelta = 45;//45 min before start of event
string savedFile = (dataDir + "calendarWithDisplayReminder.ics");
calendar.Save(savedFile, AppointmentSaveFormat.Ics);
添加音频提醒
以下代码片段演示了如何向日历添加音频提醒。
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
Appointment app = new Appointment("Home", DateTime.Now.AddHours(1), DateTime.Now.AddHours(1), "organizer@domain.com", "attendee@gmail.com");
MailMessage msg = new MailMessage();
msg.AddAlternateView(app.RequestApointment());
MapiMessage mapi = MapiMessage.FromMailMessage(msg);
MapiCalendar calendar = (MapiCalendar)mapi.ToMapiMessageItem();
// Set calendar properties
calendar.ReminderSet = true;
calendar.ReminderDelta = 58;//58 min before start of event
calendar.ReminderFileParameter = dataDir + "Alarm01.wav";
string savedFile = (dataDir + "calendarWithAudioReminder_out.ics");
calendar.Save(savedFile, AppointmentSaveFormat.Ics);
添加/检索日历文件附件
以下代码片段演示了如何添加/检索日历文件中的附件。
string[] files = new string[3];
files[0] = dataDir + "attachment_1.doc";
files[1] = dataDir + "download.png";
files[2] = dataDir + "Desert.jpg";
Appointment app1 = new Appointment("Home", DateTime.Now.AddHours(1), DateTime.Now.AddHours(1), "organizer@domain.com", "attendee@gmail.com");
foreach (string file in files)
{
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(file)))
{
app1.Attachments.Add(new Attachment(ms, Path.GetFileName(file)));
}
}
app1.Save(dataDir + "appWithAttachments_out.ics", AppointmentSaveFormat.Ics);
Appointment app2 = Appointment.Load(dataDir + "appWithAttachments_out.ics");
Console.WriteLine(app2.Attachments.Count);
foreach (Attachment att in app2.Attachments)
Console.WriteLine(att.Name);
检查会议请求中收件人的状态
以下代码片段展示了如何显示会议请求中收件人的状态。
MapiMessage message = MapiMessage.FromFile(fileName);
foreach (MapiRecipient recipient in message.Recipients)
{
Console.WriteLine(recipient.RecipientTrackStatus);
}
从标准时区创建 MAPI 日历时区
以下代码片段展示了如何从标准时区创建 MapiCalendarTimeZone。
MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone(TimeZoneInfo.Local);
为约会设置提醒
在创建约会时可以添加提醒。这些闹钟可以基于不同的条件触发,例如在计划开始前 n 分钟,或每隔 n 间隔重复 n 次。可以使用不同的标签在约会内的 BEGIN:VALARM 与 END:VALARM 包括的脚本中创建这些触发器。约会的提醒设置有多种变体。
添加标签以设置提醒
以下代码片段演示了如何通过添加标签来设置提醒。
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
string location = "Meeting Location: Room 5";
DateTime startDate = new DateTime(1997, 3, 18, 18, 30, 00),
endDate = new DateTime(1997, 3, 18, 19, 30, 00);
MailAddress organizer = new MailAddress("aaa@amail.com", "Organizer");
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add(new MailAddress("bbb@bmail.com", "First attendee"));
Appointment target = new Appointment(location, startDate, endDate, organizer, attendees);
// Audio alarm that will sound at a precise time and
// Repeat 4 more times at 15 minute intervals:
AppointmentReminder audioReminder = new AppointmentReminder();
audioReminder.Trigger = new ReminderTrigger(new DateTime(1997, 3, 17, 13, 30, 0, DateTimeKind.Utc));
audioReminder.Repeat = 4;
audioReminder.Duration = new ReminderDuration(new TimeSpan(0, 15, 0));
audioReminder.Action = ReminderAction.Audio;
ReminderAttachment attach = new ReminderAttachment(new Uri("ftp://Host.com/pub/sounds/bell-01.aud"));
audioReminder.Attachments.Add(attach);
target.Reminders.Add(audioReminder);
// Display alarm that will trigger 30 minutes before the
// Scheduled start of the event it is
// Associated with and will repeat 2 more times at 15 minute intervals:
AppointmentReminder displayReminder = new AppointmentReminder();
ReminderDuration dur = new ReminderDuration(new TimeSpan(0, -30, 0));
displayReminder.Trigger = new ReminderTrigger(dur, ReminderRelated.Start);
displayReminder.Repeat = 2;
displayReminder.Duration = new ReminderDuration(new TimeSpan(0, 15, 0));
displayReminder.Action = ReminderAction.Display;
displayReminder.Description = "Breakfast meeting with executive team at 8:30 AM EST";
target.Reminders.Add(displayReminder);
// Email alarm that will trigger 2 days before the
// Scheduled due date/time. It does not
// Repeat. The email has a subject, body and attachment link.
AppointmentReminder emailReminder = new AppointmentReminder();
ReminderDuration dur1 = new ReminderDuration(new TimeSpan(-2, 0, 0, 0));
emailReminder.Trigger = new ReminderTrigger(dur1, ReminderRelated.Start);
ReminderAttendee attendee = new ReminderAttendee("john_doe@host.com");
emailReminder.Attendees.Add(attendee);
emailReminder.Action = ReminderAction.Email;
emailReminder.Summary = "REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING";
emailReminder.Description = @"A draft agenda needs to be sent out to the attendees to the weekly managers meeting (MGR-LIST). Attached is a pointer the document template for the agenda file.";
ReminderAttachment attach1 = new ReminderAttachment(new Uri("http://Host.com/templates/agenda.doc"));
emailReminder.Attachments.Add(attach1);
target.Reminders.Add(emailReminder);
// Procedural alarm that will trigger at a precise date/time
// And will repeat 23 more times at one hour intervals. The alarm will
// Invoke a procedure file.
AppointmentReminder procReminder = new AppointmentReminder();
procReminder.Trigger = new ReminderTrigger(new DateTime(1998, 1, 1, 5, 0, 0, DateTimeKind.Utc));
procReminder.Repeat = 23;
procReminder.Duration = new ReminderDuration(new TimeSpan(1, 0, 0));
procReminder.Action = ReminderAction.Procedure;
ReminderAttachment attach2 = new ReminderAttachment(new Uri("ftp://Host.com/novo-procs/felizano.exe"));
procReminder.Attachments.Add(attach2);
target.Reminders.Add(procReminder);
target.Save(dataDir + "savedFile_out.ics");
将预约 EML 转换为带 HTML 正文的 MSG
自 19.3 版起,Aspose.Email 提供将预约 EML 转换为 MSG 的功能,并保留预约的 HTML 正文。Aspose.Email 提供了一个 MapiConversionOptions.ForcedRtfBodyForAppointment 属性,其默认值为 true. 当该值 MapiConversionOptions.ForcedRtfBodyForAppointment 被设置为 true 时,约会正文会转换为 RTF 格式。要保持约会正文的 HTML 格式,请设置 MapiConversionOptions.ForcedRtfBodyForAppointment 为 false.
以下示例演示了使用 MapiConversionOptions.ForcedRtfBodyForAppointment 属性,用于保持约会正文的 HTML 格式。
// Outlook directory
string dataDir = RunExamples.GetDataDir_Outlook();
MailMessage mailMessage = MailMessage.Load(dataDir + "TestAppointment.eml");
MapiConversionOptions conversionOptions = new MapiConversionOptions();
conversionOptions.Format = OutlookMessageFormat.Unicode;
// default value for ForcedRtfBodyForAppointment is true
conversionOptions.ForcedRtfBodyForAppointment = false;
MapiMessage mapiMessage = MapiMessage.FromMailMessage(mailMessage, conversionOptions);
Console.WriteLine("Body Type: " + mapiMessage.BodyType);
mapiMessage.Save(dataDir + "TestAppointment_out.msg");
手动设置 MAPI 日历项的状态
显式设置 MAPI 日历对象的状态,覆盖默认行为。这使得对日历事件状态有更好的控制,特别是在处理已接收的会议请求时。默认情况下,创建会议时,其状态为 MapiCalendarState.Meeting。当收到收件人收件箱时,它会自动更改为 MapiCalendarState.Received,其消息类更新为 “IPM.Schedule.Meeting.Request”。使用 SetStateForced 允许手动将状态设置为 Received,这在将日历保存为 MSG 文件时有助于保留组织者信息。但这可能会阻止会议的正确转发或重新发送。
下面的代码示例演示了如何创建一个 MapiCalendar 对象,指定组织者,并显式将其状态设置为 both Meeting 和 Received 使用 SetStateForced。随后将日历项保存为 .msg 文件。
MapiCalendar appointment = new MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
new DateTime(2024, 5, 10, 12, 30, 0, DateTimeKind.Utc),
new DateTime(2024, 5, 10, 13, 30, 0, DateTimeKind.Utc));
appointment.Organizer = new MapiElectronicAddress
{
EmailAddress = "test@aaa.com",
DisplayName = "test display Name"
};
appointment.SetStateForced(MapiCalendarState.Meeting | MapiCalendarState.Received);
appointment.Save("appointment.msg", AppointmentSaveFormat.Msg);
另见
- 管理约会 — 使用 iCalendar(ICS)约会和会议请求。
- 在 PST 文件中管理日历项 — 在 PST 中存储和读取日历项。