在 C++ 中创建、保存和读取 Outlook 任务
Aspose.Email for C++ 使开发者能够以编程方式创建、修改和读取 Microsoft Outlook 任务。 MapiTask 类提供的属性包括 PercentComplete, EstimatedEffort, ActualEffort, History, LastUpdate,以及更多功能,使您能够完整定义任务细节。
本文介绍了如何创建、保存和读取 Outlook 任务,包括处理 VToDo 任务、提醒、附件和重复模式。
本文中的示例使用了以下头文件和名称空间:
#include <MailMessage.h>
#include <SaveOptions.h>
#include <MhtSaveOptions.h>
#include <HtmlSaveOptions.h>
#include <MhtFormatOptions.h>
#include <HtmlFormatOptions.h>
#include <MhtTemplateName.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiItemType.h>
#include <Mapi/MapiTask.h>
#include <Mapi/MapiTaskUsers.h>
#include <Mapi/MapiTaskHistory.h>
#include <Mapi/MapiTaskOwnership.h>
#include <Mapi/MapiTaskStatus.h>
#include <Mapi/MapiTaskState.h>
#include <Mapi/TaskSaveFormat.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <Mapi/MapiCalendarRecurrencePattern.h>
#include <Mapi/MapiCalendarWeeklyRecurrencePattern.h>
#include <Mapi/MapiCalendarRecurrencePatternType.h>
#include <Mapi/MapiCalendarRecurrenceEndType.h>
#include <Mapi/MapiCalendarDayOfWeek.h>
#include <Storage/Pst/MapiSensitivity.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/collections/specialized/string_dictionary.h>
#include <system/text/encoding.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
创建并保存 Outlook 任务
要创建任务并以 MSG 格式保存,请按以下步骤操作:
- 实例化一个 MapiTask ——构造函数接受主题、正文、开始日期和截止日期。
- 设置所需的任务属性。
- 使用以下对象保存任务 MapiTask::Save 和
TaskSaveFormat::Msg.
除了进度和工作量数字外,任务还记录所有者和指派人(MapiTaskUsers), 对其进行的最后一次更改(MapiTaskHistory), 其 MapiTaskStatus 和敏感度,以及 Outlook 在详细信息页上显示的公司、类别、里程和计费信息。
SharedPtr<MapiTask> task = MakeObject<MapiTask>(u"To Do", u"Just click and type to add new task", DateTime(2025, 5, 5), DateTime(2025, 5, 8));
task->set_PercentComplete(20);
task->set_EstimatedEffort(2000);
task->set_ActualEffort(20);
task->set_History(MapiTaskHistory::Assigned);
task->set_LastUpdate(DateTime(2025, 5, 6, 9, 0, 0));
task->get_Users()->set_Owner(u"Darius");
task->get_Users()->set_LastAssigner(u"Harkness");
task->get_Users()->set_LastDelegate(u"Harkness");
task->get_Users()->set_Delegator(u"Test Delegator");
task->get_Users()->set_Ownership(MapiTaskOwnership::AssignersCopy);
task->set_Companies(MakeArray<String>({u"company1", u"company2", u"company3"}));
task->set_Categories(MakeArray<String>({u"category1", u"category2", u"category3"}));
task->set_Mileage(u"Some test mileage");
task->set_Billing(u"Test billing information");
task->set_Sensitivity(Storage::Pst::MapiSensitivity::Personal);
task->set_Status(MapiTaskStatus::InProgress);
task->Save(u"Task.msg", TaskSaveFormat::Msg);
从磁盘读取 MapiTask
以 MSG 文件保存的任务使用 MapiMessage::Load,以及 ToMapiMessageItem 将其转换为 MapiTask。 检查 get_SupportedType 首先确保文件确实包含任务 - DynamicCast 当项目是其他类型时抛出异常。
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"Task.msg");
if (msg->get_SupportedType() == MapiItemType::Task)
{
SharedPtr<MapiTask> mapiTask = DynamicCast<MapiTask>(msg->ToMapiMessageItem());
Console::WriteLine(u"Subject: " + mapiTask->get_Subject());
Console::WriteLine(u"Due: " + mapiTask->get_DueDate().ToString());
Console::WriteLine(u"Done: " + Convert::ToString(mapiTask->get_PercentComplete()) + u"%");
Console::WriteLine(u"Owner: " + mapiTask->get_Users()->get_Owner());
}
加载 VToDo 任务(iCalendar)
从其他应用导出的任务——例如 Google Tasks——通常以 iCalendar 文件形式出现,包含 VTODO 组件。 MapiTask::FromVTodo 将此类文件读取到一个 MapiTask,随后可保存为 Outlook 任务。
SharedPtr<MapiTask> task = MapiTask::FromVTodo(u"VtodoTask.ics");
task->Save(u"VToDo_out.msg", TaskSaveFormat::Msg);
Console::WriteLine(u"Imported task: " + task->get_Subject() + u", due " + task->get_DueDate().ToString());
向任务添加提醒信息
如同在 Microsoft Outlook 中,任务可以在截止前提醒所有者。 设置_ReminderSet 打开提醒, 设置_ReminderTime 指示何时触发,并且 设置_ReminderFileParameter 为 Outlook 播放的声音文件命名。
SharedPtr<MapiTask> task = MakeObject<MapiTask>(u"Task with reminder", u"This is a body", DateTime(2025, 5, 5, 9, 0, 0), DateTime(2025, 5, 5, 10, 0, 0));
task->set_ReminderSet(true);
task->set_ReminderTime(DateTime(2025, 5, 5, 8, 45, 0));
task->set_ReminderFileParameter(u"Alarm01.wav");
task->Save(u"TaskWithReminder.msg", TaskSaveFormat::Msg);
向任务添加附件
属于任务的文件作为附件随任务一起移动。该 MapiAttachmentCollection 任务的名称和每个附件的内容。
SharedPtr<MapiTask> task = MakeObject<MapiTask>(u"Task with attachment", u"Test body of task with attachment", DateTime(2025, 5, 5, 9, 0, 0), DateTime(2025, 5, 5, 10, 0, 0));
task->get_Attachments()->Add(u"Test attachment.txt", Text::Encoding::get_UTF8()->GetBytes(u"Test attachment body"));
task->Save(u"TaskWithAttachment.msg", TaskSaveFormat::Msg);
Console::WriteLine(u"Attachments: " + Convert::ToString(MapiMessage::Load(u"TaskWithAttachment.msg")->get_Attachments()->get_Count()));
向任务添加重复
重复任务在其 重复 属性中有详细描述,每日、每周和每月模式 - MapiCalendarDailyRecurrencePattern, MapiCalendarWeeklyRecurrencePattern 和 MapiCalendarMonthlyRecurrencePattern,周期为十二个月时也覆盖年度系列——在 管理循环。此示例将任务设为每周三重复。
SharedPtr<MapiTask> task = MakeObject<MapiTask>(u"Weekly report", u"Send the weekly report", DateTime(2025, 4, 30, 10, 0, 0), DateTime(2025, 4, 30, 11, 0, 0));
task->set_State(MapiTaskState::NotAssigned);
SharedPtr<MapiCalendarWeeklyRecurrencePattern> weekly = MakeObject<MapiCalendarWeeklyRecurrencePattern>();
weekly->set_PatternType(MapiCalendarRecurrencePatternType::Week);
weekly->set_Period(1);
weekly->set_WeekStartDay(DayOfWeek::Sunday);
weekly->set_DayOfWeek(MapiCalendarDayOfWeek::Wednesday);
weekly->set_EndType(MapiCalendarRecurrenceEndType::NeverEnd);
task->set_Recurrence(weekly);
task->Save(u"WeeklyTask.msg", TaskSaveFormat::Msg);
在 MHTML 和 HTML 中呈现任务字段
以 MHTML 或 HTML 保存的任务通常只显示普通邮件的字段。 RenderTaskFields - 同时属于两者的成员 MhtFormatOptions 和 HtmlFormatOptions - 添加特定任务的字段:状态、所有者、计划工作和实际工作。每行的标记来自于 格式模板 字典,以常量为键,来源于 MhtTemplateName::Task 类中;先清空字典可确保仅保留您添加的行。
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"Task.msg");
SharedPtr<MhtSaveOptions> options = SaveOptions::get_DefaultMhtml();
options->set_MhtFormatOptions(MhtFormatOptions::RenderTaskFields | MhtFormatOptions::WriteHeader);
// the task fields written into the header block and their markup
options->get_FormatTemplates()->Clear();
options->get_FormatTemplates()->Add(MhtTemplateName::Task::Subject,
u"<span class='headerLineTitle'>Subject:</span><span class='headerLineText'>{0}</span><br/>");
options->get_FormatTemplates()->Add(MhtTemplateName::Task::ActualWork,
u"<span class='headerLineTitle'>Actual Work:</span><span class='headerLineText'>{0}</span><br/>");
options->get_FormatTemplates()->Add(MhtTemplateName::Task::TotalWork,
u"<span class='headerLineTitle'>Total Work:</span><span class='headerLineText'>{0}</span><br/>");
options->get_FormatTemplates()->Add(MhtTemplateName::Task::Status,
u"<span class='headerLineTitle'>Status:</span><span class='headerLineText'>{0}</span><br/>");
options->get_FormatTemplates()->Add(MhtTemplateName::Task::Owner,
u"<span class='headerLineTitle'>Owner:</span><span class='headerLineText'>{0}</span><br/>");
msg->Save(u"MapiTask.mht", options);
// the same fields can be written when the task is exported to HTML
SharedPtr<HtmlSaveOptions> htmlOptions = SaveOptions::get_DefaultHtml();
htmlOptions->set_HtmlFormatOptions(HtmlFormatOptions::WriteHeader | HtmlFormatOptions::RenderTaskFields);
msg->Save(u"MapiTask.html", htmlOptions);