管理循环

Outlook 将重复约会或任务的计划存储在 重复模式 中。Aspose.Email for C++ 使用以下类对其建模 Aspose::Email::Mapi 命名空间: MapiCalendarDailyRecurrencePattern, MapiCalendarWeeklyRecurrencePattern 和 MapiCalendarMonthlyRecurrencePattern,全部派生自 MapiCalendarRecurrencePattern. 完成的模式被分配给 Recurrence 属性属于 MapiTask 或,通过 MapiCalendarEventRecurrence,属于一个 MapiCalendar.

每个模式都由相同的一组属性描述:

| 属性 | 含义 | | — | — | | PatternType | 调度的单位: MapiCalendarRecurrencePatternType Day, Week, Month, MonthNth 等等。 | | Period | 两次出现之间的单位数。 | | WeekStartDay | 每周模式的第一天。 | | EndType | 系列的结束方式: MapiCalendarRecurrenceEndType EndAfterNOccurrences, EndAfterDate 或 NeverEnd. | | OccurrenceCount | 系列在计数后结束时的出现次数。 | | 异常 | 与系列其余部分不同的出现次数。 |

下面的示例使用这些头文件和命名空间:

#include <Calendar/AppointmentSaveFormat.h>
#include <Calendar/Recurrences/CalendarRecurrence.h>
#include <Calendar/Recurrences/DateCollection.h>
#include <Mapi/MapiTask.h>
#include <Mapi/MapiTaskState.h>
#include <Mapi/TaskSaveFormat.h>
#include <Mapi/MapiCalendar.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <Mapi/MapiCalendarEventRecurrence.h>
#include <Mapi/MapiCalendarExceptionInfo.h>
#include <Mapi/MapiCalendarRecurrencePattern.h>
#include <Mapi/MapiCalendarRecurrencePatternFactory.h>
#include <Mapi/MapiCalendarDailyRecurrencePattern.h>
#include <Mapi/MapiCalendarWeeklyRecurrencePattern.h>
#include <Mapi/MapiCalendarMonthlyRecurrencePattern.h>
#include <Mapi/MapiCalendarRecurrencePatternType.h>
#include <Mapi/MapiCalendarRecurrenceEndType.h>
#include <Mapi/MapiCalendarDayOfWeek.h>
#include <system/text/encoding.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Calendar;
using namespace Aspose::Email::Calendar::Recurrences;

具有固定出现次数的每日系列

Outlook 将 “在此日期之前重复” 表示为出现次数而非结束日期,因此需要先计算该次数以在给定日期停止系列。 CalendarRecurrence 进行算术运算:从 RRULE 字符串构建并展开到所需的结束日期。

// Counts the occurrences a rule produces up to the given date
uint32_t GetOccurrenceCount(DateTime start, DateTime endBy, String rrule)
{
    SharedPtr<CalendarRecurrence> pattern = MakeObject<CalendarRecurrence>(
        String::Format(u"DTSTART:{0}\r\nRRULE:{1}", start.ToString(u"yyyyMMdd"), rrule));

    // the overload that takes only the end of the range counts from DTSTART
    SharedPtr<DateCollection> dates = pattern->GenerateOccurrences(endBy);
    return static_cast<uint32_t>(dates->get_Count());
}
DateTime startDate(2025, 7, 16);
DateTime dueDate(2025, 7, 16);
DateTime endByDate(2025, 8, 1);

SharedPtr<MapiTask> task = MakeObject<MapiTask>(u"This is a test task", u"Sample Body", startDate, dueDate);
task->set_State(MapiTaskState::NotAssigned);

// every day, ending after a fixed number of occurrences
SharedPtr<MapiCalendarDailyRecurrencePattern> recurrence = MakeObject<MapiCalendarDailyRecurrencePattern>();
recurrence->set_PatternType(MapiCalendarRecurrencePatternType::Day);
recurrence->set_Period(1);
recurrence->set_WeekStartDay(DayOfWeek::Sunday);
recurrence->set_EndType(MapiCalendarRecurrenceEndType::EndAfterNOccurrences);
recurrence->set_OccurrenceCount(GetOccurrenceCount(startDate, endByDate, u"FREQ=DAILY"));

if (recurrence->get_OccurrenceCount() == 0)
{
    recurrence->set_OccurrenceCount(1);
}

task->set_Recurrence(recurrence);
task->Save(u"DailyTask.msg", TaskSaveFormat::Msg);

Console::WriteLine(u"Occurrences: " + Convert::ToString(recurrence->get_OccurrenceCount()));

每周、每月和每年系列

每周模式添加 DayOfWeek,一个标志枚举,其中多个天数组合为 |。月度模式添加 Day,系列所在的月份的天数。年度系列是周期为十二个月的月度模式。 NeverEnd 让系列保持开放结束,在这种情况下 OccurrenceCount 不使用。

DateTime startDate(2025, 7, 16);
DateTime dueDate(2025, 7, 16);

SharedPtr<MapiTask> task = MakeObject<MapiTask>(u"This is a test task", u"Sample Body", startDate, dueDate);
task->set_State(MapiTaskState::NotAssigned);

// every second week, on Monday and Friday, ten times
SharedPtr<MapiCalendarWeeklyRecurrencePattern> weekly = MakeObject<MapiCalendarWeeklyRecurrencePattern>();
weekly->set_PatternType(MapiCalendarRecurrencePatternType::Week);
weekly->set_Period(2);
weekly->set_WeekStartDay(DayOfWeek::Sunday);
weekly->set_DayOfWeek(MapiCalendarDayOfWeek::Monday | MapiCalendarDayOfWeek::Friday);
weekly->set_EndType(MapiCalendarRecurrenceEndType::EndAfterNOccurrences);
weekly->set_OccurrenceCount(10);
task->set_Recurrence(weekly);
task->Save(u"WeeklyTask.msg", TaskSaveFormat::Msg);

// the 16th day of every month, without an end
SharedPtr<MapiCalendarMonthlyRecurrencePattern> monthly = MakeObject<MapiCalendarMonthlyRecurrencePattern>();
monthly->set_PatternType(MapiCalendarRecurrencePatternType::Month);
monthly->set_Period(1);
monthly->set_Day(16);
monthly->set_EndType(MapiCalendarRecurrenceEndType::NeverEnd);
task->set_Recurrence(monthly);
task->Save(u"MonthlyTask.msg", TaskSaveFormat::Msg);

// the same day every year: a monthly pattern with a period of twelve months
SharedPtr<MapiCalendarMonthlyRecurrencePattern> yearly = MakeObject<MapiCalendarMonthlyRecurrencePattern>();
yearly->set_PatternType(MapiCalendarRecurrencePatternType::Month);
yearly->set_Period(12);
yearly->set_Day(1);
yearly->set_EndType(MapiCalendarRecurrenceEndType::EndAfterNOccurrences);
yearly->set_OccurrenceCount(5);
task->set_Recurrence(yearly);
task->Save(u"YearlyTask.msg", TaskSaveFormat::Msg);

从 RRULE 字符串构建模式

当计划已作为 iCalendar 文本存在 —— 来自 ICS 文件或其他系统时 —— MapiCalendarRecurrencePatternFactory::FromString 根据 RFC 5545 解析并生成 MAPI 模式。 EXDATE 条目最终位于 DeletedInstanceDates.

DateTime startDate(2025, 7, 16);
DateTime endDate(2025, 8, 1);

SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"test location", u"test summary",
    u"test description", startDate, endDate);

String pattern =
    u"DTSTART;TZID=Europe/London:20250831T080000\r\n"
    u"DTEND;TZID=Europe/London:20250831T083000\r\n"
    u"RRULE:FREQ=DAILY;INTERVAL=1;COUNT=7\r\n"
    u"EXDATE:20250902T070000Z,20250904T070000Z";

// the RRULE text is turned into a MAPI recurrence pattern
calendar->get_Recurrence()->set_RecurrencePattern(MapiCalendarRecurrencePatternFactory::FromString(pattern));

calendar->Save(u"FromRRule.msg", AppointmentSaveFormat::Msg);

SharedPtr<MapiCalendarRecurrencePattern> parsed = calendar->get_Recurrence()->get_RecurrencePattern();
Console::WriteLine(u"Occurrences: " + Convert::ToString(parsed->get_OccurrenceCount()) +
    u", deleted instances: " + Convert::ToString(parsed->get_DeletedInstanceDates()->get_Count()));

上述解析的模式报告了七个实例和两个已删除的实例。

为单个实例提供独立内容

系列的单个实例可能与其余不同 —— 例如不同的会议室、不同的主题、附带议程的会议。此类偏差称为 MapiCalendarExceptionInfo 对象添加到 异常 模式的集合。 OriginalStartDate 标识被替换的实例,异常自身携带其附件集合。

DateTime startDate = DateTime(2025, 7, 19).AddHours(12);

SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"location1", u"summary1", u"description1",
    startDate, startDate.AddHours(1));

SharedPtr<MapiCalendarEventRecurrence> recurrence = MakeObject<MapiCalendarEventRecurrence>();
SharedPtr<MapiCalendarDailyRecurrencePattern> pattern = MakeObject<MapiCalendarDailyRecurrencePattern>();
pattern->set_PatternType(MapiCalendarRecurrencePatternType::Day);
pattern->set_Period(1);
pattern->set_EndType(MapiCalendarRecurrenceEndType::NeverEnd);
recurrence->set_RecurrencePattern(pattern);

// one occurrence differs from the rest of the series
DateTime exceptionDate = startDate.AddDays(3);
SharedPtr<MapiCalendarExceptionInfo> exception = MakeObject<MapiCalendarExceptionInfo>();
exception->set_Location(u"exceptionLoc");
exception->set_Subject(u"exceptionSubj");
exception->set_Body(u"exceptionBody");
exception->set_OriginalStartDate(exceptionDate);
exception->set_StartDateTime(exceptionDate);
exception->set_EndDateTime(exceptionDate.AddHours(5));

// and carries an attachment of its own
exception->set_Attachments(MakeObject<MapiAttachmentCollection>(calendar));
exception->get_Attachments()->Add(u"file.txt", System::Text::Encoding::get_ASCII()->GetBytes(u"hello, world!"));

pattern->get_Exceptions()->Add(exception);
pattern->get_ModifiedInstanceDates()->Add(exceptionDate);

calendar->set_Recurrence(recurrence);
calendar->Save(u"RecurrenceWithException.msg", AppointmentSaveFormat::Msg);

SharedPtr<MapiCalendar> reloaded = DynamicCast<MapiCalendar>(MapiMessage::Load(u"RecurrenceWithException.msg")->ToMapiMessageItem());
Console::WriteLine(u"Exceptions: " +
    Convert::ToString(reloaded->get_Recurrence()->get_RecurrencePattern()->get_Exceptions()->get_Count()));

另见