管理 Outlook 日历项
该 MapiCalendar Aspose.Email 库中的类提供了多种方法和属性来配置和操作日历项。这使得对 Outlook 日历项的有效管理成为可能,提升了生产力和组织性。以下部分将指导您了解管理日历项的不同方面。
创建并保存日历项
创建并保存日历项
以下代码片段演示了如何创建并以 ICS 格式保存日历项:
- 定义日历文件将被存储的路径。
- 通过初始化新的创建约会 MapiCalendar 包含位置、主题、描述、开始和结束时间等特定细节的对象。
- 将创建的约会保存为指定目录中的 ICS 文件。
import aspose.email as ae
from datetime import datetime
data_dir = "path/to/data/directory/"
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2012, 4, 1),
datetime(2012, 5, 1))
calendar.save(data_dir + "CalendarItem_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
将日历项保存为 MSG
以下代码片段展示了如何将日历项保存为 MSG:
import aspose.email as ae
from datetime import datetime
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2026, 6, 1, 21, 30, 0),
datetime(2026, 6, 1, 21, 50, 0))
calendar.save(data_dir + "CalendarItem_out.msg", ae.calendar.AppointmentSaveFormat.MSG)
为 MapiCalendar 到 ICS 转换设置产品 ID
Aspose.Email 库允许您使用特定选项将 MapiCalendar(日历项)保存为 ICS(iCalendar)文件。以下是 属性 将允许您保留与日历项关联的原始日期和时间戳,并在 ICS 文件中设置产品标识符,例如设置为 "Foo Ltd":
- ics_save_options.keep_original_date_time_stamp
- ics_save_options.product_identifier
产品标识符是用于标识生成该 ICS 文件的软件或服务的字段。
以下代码示例演示了在将 MapiCalendar 保存为 ICS 时设置产品 ID:
import aspose.email as ae
from datetime import datetime
mapi_calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2024, 4, 1),
datetime(2024, 4, 2))
ics_save_options = ae.mapi.MapiCalendarIcsSaveOptions()
ics_save_options.keep_original_date_time_stamp = True
ics_save_options.product_identifier = "Foo Ltd"
mapi_calendar.save("my.ics", ics_save_options)
读取并设置日历项的产品标识符
iCalendar 文件在 PRODID 字段。Aspose.Email 在日历项在 ICS 与 MSG 之间转换时保留此标识符,以便您追踪该项的来源。该标识符可通过以下属性获取:
product_id的 Appointment 类 - 获取或设置创建约会的产品标识符。product_id的 MapiCalendar 类 - 获取或设置创建日历项的产品标识符。product_identifier的 MapiCalendarMsgSaveOptions 和 MapiCalendarIcsSaveOptions 类 - 设置保存日历项时写入的标识符。
下面的代码示例从 ICS 文件读取产品标识符,将约会转换为 MSG 并 AppointmentMsgSaveOptions,使用新标识符保存日历项,然后将另一个标识符写入 ICS 文件:
import io
import aspose.email as ae
from aspose.pycore import cast
# Read the product identifier from an iCalendar file
appointment = ae.calendar.Appointment.load("meeting.ics")
print("Appointment product ID:", appointment.product_id)
# The identifier is kept when the appointment is converted to MSG
msg_stream = io.BytesIO()
appointment.save(msg_stream, ae.calendar.AppointmentMsgSaveOptions())
msg_stream.seek(0)
msg = ae.mapi.MapiMessage.load(msg_stream)
if msg.supported_type == ae.mapi.MapiItemType.CALENDAR:
calendar = cast(ae.mapi.MapiCalendar, msg.to_mapi_message_item())
print("MapiCalendar product ID:", calendar.product_id)
# Save the calendar item to MSG with a different product identifier
options = ae.mapi.MapiCalendarMsgSaveOptions()
options.product_identifier = "-//Contoso//Calendar 2.0//EN"
calendar.save("meeting_out.msg", options)
# The identifier set on the item is written to the PRODID field of an ICS file
calendar.product_id = "-//Contoso//Calendar 3.0//EN"
calendar.save("meeting_out.ics", ae.mapi.MapiCalendarIcsSaveOptions())
统计日历总事件数
该 CalendarReader 该类提供读取和分析指定文件中日历数据的功能。通过初始化 CalendarReader 对象,您可以提取关键细节,如事件总数、日历版本、使用的方法以及日历是否包含多个事件。以下代码片段演示了如何与日历数据交互并将约会加载为完整的事件列表:
import aspose.email as ae
reader = ae.calendar.CalendarReader("calendar.ics")
print(f"Calendar contains {reader.count} events")
print(f"The Version of the calendar is {reader.version}")
print(f"The Method of the calendar is {reader.method}")
print(f"Does the calendar contain multiple events? - {reader.is_multi_events}")
appointments = reader.load_as_multiple()
向日历项添加显示提醒
下面的代码片段展示了如何为日历项添加显示提醒:
import aspose.email as ae
from datetime import datetime
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2026, 6, 1, 21, 30, 0),
datetime(2026, 6, 1, 21, 50, 0))
calendar.reminder_set = True
calendar.reminder_delta = 45 # 45 minutes before the start of the event
calendar.save(data_dir + "AddDisplayReminderToCalendar_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
calendar.save(data_dir + "AddDisplayReminderToCalendar_out.msg", ae.calendar.AppointmentSaveFormat.MSG)
向日历项添加音频提醒
下面的代码片段展示了如何为日历项添加音频提醒:
import aspose.email as ae
from datetime import datetime
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2026, 6, 1, 21, 30, 0),
datetime(2026, 6, 1, 21, 50, 0))
calendar.reminder_set = True
calendar.reminder_delta = 45 # 45 minutes before the start of the event
calendar.reminder_file_parameter = "file://Alarm01.wav"
calendar.save(data_dir + "AddAudioReminderToCalendar_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
显式设置 MapiCalendar 状态
Aspose.Email 提供了一种方法,可显式定义 MapiCalendar 对象的状态,覆盖默认行为:
- void MapiCalendar.set_state_forced(MapiCalendarState state) - 在处理已接收的会议请求时,提供了对日历项状态的更大控制。
当创建 MapiCalendar 对象时,它会自动分配状态 MapiCalendarState.MEETING。当收件人收到该项时,状态通常会更改为 MapiCalendarState.RECEIVED,并且消息类会更新为 "IPM.Schedule.Meeting.Request"。
以下代码示例演示了如何使用 Aspose.Email for Python via .NET 创建会议约会,手动设置组织者和日历状态,并将约会保存为 Outlook .msg 文件:
from aspose.email.mapi import MapiCalendar, MapiCalendarState, MapiElectronicAddress
from aspose.email.calendar import AppointmentSaveFormat
from datetime import datetime, timezone
# Create a MapiCalendar appointment
appointment = MapiCalendar(
"LAKE ARGYLE WA 6743", # Location
"Appointment", # Subject
"This is a very important meeting :)", # Body
datetime(2024, 5, 10, 12, 30, 0, tzinfo=timezone.utc), # Start time (UTC)
datetime(2024, 5, 10, 13, 30, 0, tzinfo=timezone.utc) # End time (UTC)
)
# Set the organizer
appointment.organizer = MapiElectronicAddress()
appointment.organizer.email_address = "test@aaa.com"
appointment.organizer.display_name = "test display Name"
# Forcefully set the calendar state to Meeting | Received
appointment.set_state_forced(MapiCalendarState.MEETING | MapiCalendarState.RECEIVED)
# Save the appointment to a .msg file
appointment.save("appointment.msg", AppointmentSaveFormat.MSG)
通过调用 set_state_forced,您可以手动将日历项的状态设置为特定值(例如 MapiCalendarState.RECEIVED)。这在您想要以下操作时特别有用:
-
模拟收到的会议请求。
-
在将约会保存为 .MSG 文件时保留组织者和会议元数据。
-
在默认行为不足时自定义日历工作流。
注意: 使用手动设置状态与
set_state_forced可能影响 Outlook 的行为,例如转发或重新发送会议的能力。因此,应谨慎使用,仅在应用程序逻辑确实需要时使用。
日历文件管理
在日历文件中添加或检索附件
Aspose.Email 日历模块(aspose.email.calendar) 还提供添加和检索附件信息的功能。
要在约会中包含附件,请按照以下步骤操作:
- 创建一个 Attachment 提供文件路径的对象。
- 将此附件追加到约会的附件列表中。
- 使用以下方式将约会保存到特定文件路径的 iCalendar 格式 保存 方法以及适当的文件格式。
要从约会中检索附件,请执行以下步骤:
- 使用以下方式从已保存的文件加载约会 load 方法。
- 使用 .attachments.length 属性获取约会中的附件数量。
- 遍历每个附件。
- 在循环中使用 .name 属性打印每个附件的名称。
以下代码片段演示了如何使用 "ae.calendar" 模块创建带附件的约会、保存它们并检索附件信息。它突出显示了在日历应用程序中处理约会和附件的功能与能力。
import aspose.email as ae
import datetime as dt
# Add an attachment to an appointment
attendees = ae.MailAddressCollection()
attendees.append(ae.MailAddress("attendee@domain.com", "Recipient 1"))
appointment = ae.calendar.Appointment("Room 112", dt.datetime(2023, 5, 27), dt.date(2023, 5, 28), ae.MailAddress("organizer@domain.com"), attendees)
attachment = ae.Attachment(data_dir + "Attachment.txt")
appointment.attachments.append(attachment)
appointment.save(data_dir + "appWithAttachments_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
# Retrieve attachments from an appointment
appointment2 = ae.calendar.Appointment.load(data_dir + "appWithAttachments_out.ics")
print(len(appointment2.attachments))
for att in appointment2.attachments:
print(att.name)
从会议请求中检索收件人状态
Aspose.Email 允许您处理以 MapiCalendar 对象表示的会议请求,并从这些会议请求中检索收件人状态(出席跟踪)。以下代码片段展示了如何识别每个收件人的电子邮件状态:
from aspose.email.mapi import MapiMessage
message = MapiMessage.load(data_dir + "MeetingRequest.msg")
for recipient in message.recipients:
print(f"{recipient.display_name}: {recipient.recipient_track_status}")
在保留 HTML 正文的情况下将 EML 约会转换为 MSG
Aspose.Email 使得将 EML 约会转换为 MSG 并保留 HTML 正文成为可能。forced_rtf_body_for_appointment 属性 MapiConversionOptions 该类用于操作约会正文的类型。当设置为 True 时,正文默认会转换为 RTF 格式。要保留正文为 HTML 格式,应设置为 False。以下代码示例展示了在将 EML 转换为 MSG 时如何保留约会的 HTML 正文:
import aspose.email as ae
eml = ae.MailMessage.load("appointment.eml")
conversionOptions = ae.mapi.MapiConversionOptions()
conversionOptions.format = ae.mapi.OutlookMessageFormat.UNICODE
# default value for ForcedRtfBodyForAppointment is true
conversionOptions.forced_rtf_body_for_appointment = False
msg = ae.mapi.MapiMessage.from_mail_message(eml, conversionOptions)
print(f"Body Type: {msg.body_type}")
msg.save("appointment.msg")
为约会设置提醒
在创建约会时可以添加提醒。这些警报可以通过不同的条件触发——在计划开始前 n 分钟、每间隔 n 分钟重复 n 次,等等。在 ICS 约会中,这些触发器由包含在 BEGIN:VALARM 和 END:VALARM.
添加标签以设置提醒
Aspose.Email 通过 reminders 集合的 Appointment class 和 AppointmentReminder, ReminderTrigger, ReminderDuration, ReminderAttachment 和 ReminderAttendee 类。警报的种类由 ReminderAction 枚举—— AUDIO, DISPLAY, EMAIL 或 PROCEDURE.
下面的代码片段展示了如何在单个约会上设置多个提醒:
import datetime
import aspose.email as ae
from aspose.email.calendar import (Appointment, AppointmentReminder, ReminderTrigger, ReminderDuration,
ReminderAction, ReminderAttachment, ReminderAttendee, ReminderRelated)
location = "Meeting Location: Room 5"
start_date = datetime.datetime(1997, 3, 18, 18, 30, 0)
end_date = datetime.datetime(1997, 3, 18, 19, 30, 0)
organizer = ae.MailAddress("aaa@amail.com", "Organizer")
attendees = ae.MailAddressCollection()
attendees.append(ae.MailAddress("bbb@bmail.com", "First attendee"))
target = Appointment(location, start_date, end_date, organizer, attendees)
# Audio alarm that will sound at a precise time and repeat 4 more times at 15 minute intervals
audio_reminder = AppointmentReminder()
audio_reminder.trigger = ReminderTrigger(datetime.datetime(1997, 3, 17, 13, 30, 0))
audio_reminder.repeat = 4
audio_reminder.duration = ReminderDuration(datetime.timedelta(minutes=15))
audio_reminder.action = ReminderAction.AUDIO
audio_reminder.attachments.append(ReminderAttachment("ftp://Host.com/pub/sounds/bell-01.aud"))
target.reminders.append(audio_reminder)
# Display alarm that will trigger 30 minutes before the scheduled start of the event
# and will repeat 2 more times at 15 minute intervals
display_reminder = AppointmentReminder()
display_reminder.trigger = ReminderTrigger(ReminderDuration(datetime.timedelta(minutes=-30)), ReminderRelated.START)
display_reminder.repeat = 2
display_reminder.duration = ReminderDuration(datetime.timedelta(minutes=15))
display_reminder.action = ReminderAction.DISPLAY
display_reminder.description = "Breakfast meeting with executive team at 8:30 AM EST"
target.reminders.append(display_reminder)
# Email alarm that will trigger 2 days before the scheduled start. It does not repeat.
email_reminder = AppointmentReminder()
email_reminder.trigger = ReminderTrigger(ReminderDuration(datetime.timedelta(days=-2)), ReminderRelated.START)
email_reminder.attendees.append(ReminderAttendee("john_doe@host.com"))
email_reminder.action = ReminderAction.EMAIL
email_reminder.summary = "REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING"
email_reminder.description = "A draft agenda needs to be sent out to the attendees of the weekly managers meeting."
email_reminder.attachments.append(ReminderAttachment("http://Host.com/templates/agenda.doc"))
target.reminders.append(email_reminder)
target.save("AppointmentWithReminders_out.ics")
MapiCalendarTimeZone 使用系统时区的 MapiCalendarTimeZone(TimeZoneInfo) 构造函数。在 Aspose.Email for Python via .NET 中 MapiCalendarTimeZone 类是只读的——它公开了 key_name 和 time_zone_rules 的时区已存储在日历项中,无法从标准时区构建。
另见
- 管理循环 — 构建每日、每周、每月和每年 MAPI 循环模式。