Working with Outlook Tasks

Creating, Saving and Reading Tasks

Aspose.Email for C++ allows you to create Outlook tasks and save them to MSG format. The MapiTask class provides a number of properties such as Percentcomplete, Estimatedeffort, ActualEffort, History, LastUpdate, and others, to accommodate and set information required for an Outlook task. This article shows how to create, save and read a MapiTask from disc. To create and save a task to disc:

  1. Instantiate a new object of the MapiTask class.
  2. Enter task property information.
  3. Save the task to disc in MSG format.

The following code snippet shows you how to create, save and read Tasks.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Outlook();
System::SharedPtr<MapiTask> task = System::MakeObject<MapiTask>(L"To Do", L"Just click and type to add new task", System::DateTime::get_Now(), System::DateTime::get_Now().AddDays(3));
task->set_PercentComplete(20);
task->set_EstimatedEffort(2000);
task->set_ActualEffort(20);
task->set_History(Aspose::Email::Outlook::MapiTaskHistory::Assigned);
task->set_LastUpdate(System::DateTime::get_Now());
task->get_Users()->set_Owner(L"Darius");
task->get_Users()->set_LastAssigner(L"Harkness");
task->get_Users()->set_LastDelegate(L"Harkness");
task->get_Users()->set_Ownership(Aspose::Email::Outlook::MapiTaskOwnership::AssignersCopy);
task->set_Companies(System::MakeArray<System::String>({L"company1", L"company2", L"company3"}));
task->set_Categories(System::MakeArray<System::String>({L"category1", L"category2", L"category3"}));
task->set_Mileage(L"Some test mileage");
task->set_Billing(L"Test billing information");
task->get_Users()->set_Delegator(L"Test Delegator");
task->set_Sensitivity(Aspose::Email::Outlook::Pst::MapiSensitivity::Personal);
task->set_Status(Aspose::Email::Outlook::MapiTaskStatus::Complete);
task->set_EstimatedEffort(5);
task->Save(dataDir + L"MapiTask.msg", Aspose::Email::Outlook::TaskSaveFormat::Msg);

Reading a MapiTask

The MapiTask class object is used to cast the MapiMessage object that loads a task from disc as MSG format. The following code snippet shows you how to reading a MapiTask.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
System::SharedPtr<MapiMessage> msg = MapiMessage::FromFile(dataDir + L"Contact.msg");
System::SharedPtr<MapiContact> mapiContact = System::DynamicCast<Aspose::Email::Outlook::MapiContact>(msg->ToMapiMessageItem());

Reading a VToDo Task

Google Tasks exported in iCalendar format as VToDo events can be loaded using the MapiTask class as shown in the following code sample. The following code snippet shows you how to reading a VToDo Task.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
System::SharedPtr<MapiTask> task = MapiTask::FromVTodo(dataDir + L"VToDoTask.ics");
task->Save(dataDir + L"VToDo_out.msg", Aspose::Email::Outlook::TaskSaveFormat::Msg);

Adding Reminder Information to a MapiTask

Similar to Microsoft Outlook, Aspose.Email can add reminder information to a MapiTask. The following code snippet shows you how to adding reminder information to a MapiTask.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Outlook();
// Create MapiTask and set Task Properties
System::SharedPtr<MapiTask> testTask = System::MakeObject<MapiTask>(L"task with reminder", L"this is a body", System::DateTime::get_Now(), System::DateTime::get_Now().AddHours(1));
testTask->set_ReminderSet(true);
testTask->set_ReminderTime(System::DateTime::get_Now());
testTask->set_ReminderFileParameter(dataDir + L"Alarm01.wav");
testTask->Save(dataDir + L"AddReminderInformationToMapiTask_out", Aspose::Email::Outlook::TaskSaveFormat::Msg);

Adding Attachments to a MapiTask

The following code snippet shows you how to add attachments to a MapiTask.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Outlook();
System::String attachmentContent = L"Test attachment body";
System::String attachmentName = L"Test attachment name";
System::SharedPtr<MapiTask> testTask = System::MakeObject<MapiTask>(L"Task with attacment", L"Test body of task with attacment", System::DateTime::get_Now(), System::DateTime::get_Now().AddHours(1));
testTask->get_Attachments()->Add(attachmentName, System::Text::Encoding::get_Unicode()->GetBytes(attachmentContent));
testTask->Save(dataDir + L"AddAttachmentsToMapiTask_out", Aspose::Email::Outlook::TaskSaveFormat::Msg);

Adding Recurrence to MapiTask

Aspose.Email allows to create a recurring task where the recurrence can be daily, weekly, monthly, or yearly. The following code snippet shows you how to creating a task with different recurrence types.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
System::DateTime startDate(2015, 4, 30, 10, 00, 00);
System::SharedPtr<MapiTask> task = System::MakeObject<MapiTask>(L"abc", L"def", startDate, startDate.AddHours(1));
task->set_State(Aspose::Email::Outlook::MapiTaskState::NotAssigned);
// Set the weekly recurrence
auto rec = System::MakeObject<MapiCalendarDailyRecurrencePattern>();
rec->set_PatternType(Aspose::Email::Outlook::MapiCalendarRecurrencePatternType::Day);
rec->set_Period(1);
rec->set_WeekStartDay(System::DayOfWeek::Sunday);
rec->set_EndType(Aspose::Email::Outlook::MapiCalendarRecurrenceEndType::NeverEnd);
rec->set_OccurrenceCount(0);
task->set_Recurrence(rec);
task->Save(dataDir + L"AsposeDaily_out.msg", Aspose::Email::Outlook::TaskSaveFormat::Msg);
// Set the weekly recurrence
auto rec1 = System::MakeObject<MapiCalendarWeeklyRecurrencePattern>();
rec1->set_PatternType(Aspose::Email::Outlook::MapiCalendarRecurrencePatternType::Week);
rec1->set_Period(1);
rec1->set_DayOfWeek(Aspose::Email::Outlook::MapiCalendarDayOfWeek::Wednesday);
rec1->set_EndType(Aspose::Email::Outlook::MapiCalendarRecurrenceEndType::NeverEnd);
rec1->set_OccurrenceCount(0);
task->set_Recurrence(rec1);
task->Save(dataDir + L"AsposeWeekly_out.msg", Aspose::Email::Outlook::TaskSaveFormat::Msg);
// Set the monthly recurrence
auto recMonthly = System::MakeObject<MapiCalendarMonthlyRecurrencePattern>();
recMonthly->set_PatternType(Aspose::Email::Outlook::MapiCalendarRecurrencePatternType::Month);
recMonthly->set_Period(1);
recMonthly->set_EndType(Aspose::Email::Outlook::MapiCalendarRecurrenceEndType::NeverEnd);
recMonthly->set_Day(30);
recMonthly->set_OccurrenceCount(0);
recMonthly->set_WeekStartDay(System::DayOfWeek::Sunday);
task->set_Recurrence(recMonthly);
//task.Save(dataDir + "AsposeMonthly_out.msg", TaskSaveFormat.Msg);
// Set the yearly recurrence
auto recYearly = System::MakeObject<MapiCalendarMonthlyRecurrencePattern>();
recYearly->set_PatternType(Aspose::Email::Outlook::MapiCalendarRecurrencePatternType::Month);
recYearly->set_EndType(Aspose::Email::Outlook::MapiCalendarRecurrenceEndType::NeverEnd);
recYearly->set_OccurrenceCount(10);
recYearly->set_Period(12);
task->set_Recurrence(recYearly);
//task.Save(dataDir + "AsposeYearly_out.msg", TaskSaveFormat.Msg);