管理 Outlook 日历项

该 MapiCalendar Aspose.Email 库中的类提供了多种方法和属性来配置和操作日历项。这使得对 Outlook 日历项的有效管理成为可能,提升了生产力和组织性。以下部分将指导您了解管理日历项的不同方面。

创建并保存日历项

创建并保存日历项

以下代码片段演示了如何创建并以 ICS 格式保存日历项:

  1. 定义日历文件将被存储的路径。
  2. 通过初始化新的创建约会 MapiCalendar 包含位置、主题、描述、开始和结束时间等特定细节的对象。
  3. 将创建的约会保存为指定目录中的 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:

为 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)

统计日历总事件数

该 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"Is calendar contains contains multiple events? - {reader.is_multi_events}")
appointments = reader.load_as_multiple()

向日历项添加显示提醒

以下代码片段演示了如何向日历添加显示提醒。

向日历项添加音频提醒

以下代码片段展示了如何向日历添加音频提醒。

显式设置 MapiCalendar 状态

Aspose.Email 提供了一种方法,可显式定义 MapiCalendar 对象的状态,覆盖默认行为:

当创建 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 Calendar("ae.calendar")模块还提供添加和检索附件信息的功能。

要在约会中包含附件,请按照以下步骤操作:

  1. 创建一个 Attachment 提供文件路径的对象。
  2. 将此附件追加到约会的附件列表中。
  3. 使用以下方式将约会保存到特定文件路径的 iCalendar 格式 保存 方法以及适当的文件格式。

要从约会中检索附件,请执行以下步骤:

  1. 使用以下方式从已保存的文件加载约会 load 方法。
  2. 使用 .attachments.length 属性获取约会中的附件数量。
  3. 遍历每个附件。
  4. 在循环中使用 .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("D:\\Aspose\\Files\\Attachment.txt")
appointment.attachments.append(attachment)

appointment.save("D:\\Aspose\\Files\\appWithAttachments_out.ics", ae.calendar.AppointmentSaveFormat.ICS)

# Retrieve attachments from an appointment 
appointment2 = ae.calendar.Appointment.load("D:\\Aspose\\Files\\appWithAttachments_out.ics")
print(appointment2.attachments.length)
for att in appointment2.attachments:
    print(att.name)

从会议请求中检索收件人状态

Aspose.Email 允许您处理以 MapiCalendar 对象表示的会议请求,并从这些会议请求中检索收件人状态(出席跟踪)。以下代码片段展示了如何识别每个收件人的电子邮件状态:

在保留 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")