Working with Outlook Tasks

Creating, Saving and Reading Tasks

Aspose.Email for .NET 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 MapiContact 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/aspose-email/aspose-email-python-dotnet
task = MapiTask("To Do", "Just click and type to add new task", dt.datetime(2018, 6, 1, 21, 30, 0), dt.datetime(2018, 6, 4, 21, 30, 0))
task.percent_complete = 20
task.estimated_effort = 2000
task.actual_effort = 20
task.history = MapiTaskHistory.ASSIGNED
task.last_update = dt.datetime(2018, 6, 1, 21, 30, 0)
task.users.owner = "Darius"
task.users.last_assigner = "Harkness"
task.users.last_delegate = "Harkness"
task.users.ownership = MapiTaskOwnership.ASSIGNERS_COPY
task.companies = [ "company1", "company2", "company3" ]
task.categories = [ "category1", "category2", "category3" ]
task.mileage = "Some test mileage"
task.billing = "Test billing information"
task.users.delegator = "Test Delegator"
task.sensitivity = MapiSensitivity.PERSONAL
task.status = MapiTaskStatus.COMPLETE
task.estimated_effort = 5
task.save(dataDir + "CreatingAndSavingOutlookTask_out.msg", TaskSaveFormat.MSG)

Reading a MapiTask

The MapiContact 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/aspose-email/aspose-email-python-dotnet
message = MapiMessage.from_file("Contact.msg")
apiContact = message.to_mapi_message_item()

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.

import aspose.email as ae

data_dir = "path/to/data/directory"

task = ae.mapi.MapiTask.from_v_todo(data_dir + "VToDoTask.ics")
task.save(data_dir + "VToDo_out.msg", ae.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/aspose-email/aspose-email-python-dotnet
task = MapiTask("To Do", "Just click and type to add new task", dt.datetime(2018, 6, 1, 21, 30, 0), dt.datetime(2018, 6, 4, 21, 30, 0))
task.reminder_set = True
task.reminder_time = dt.datetime(2018, 6, 1, 21, 30, 0)
task.reminder_file_parameter =dataDir + "file://Alarm01.wav"
task.save(dataDir + "AddReminderInformationToMapiTask_out.msg", TaskSaveFormat.MSG)

Adding Attachments to a MapiTask

Use the Add method of the MapiAttachmentCollection class to add an attachment to a MapiTask. The following code sample will help you with that:

import aspose.email as ae
import datetime as dt

task = ae.mapi.MapiTask("Task with attacment", "Test body of task with attacment", dt.datetime.now(), dt.datetime.now());
task.attachments.add("Attachment.txt", str.encode("attachment data"))
task.save("AddAttachmentsToMapiTask_out", ae.mapi.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/aspose-email/aspose-email-python-dotnet
startDate = dt.datetime(2015, 4, 30, 10, 00, 00)
task = MapiTask("abc", "def", startDate, dt.datetime(2015, 4, 30, 11, 00, 00))
task.state = MapiTaskState.NOT_ASSIGNED
# Set the weekly recurrence
rec = MapiCalendarDailyRecurrencePattern()
rec.pattern_type = MapiCalendarRecurrencePatternType.DAY
rec.period = 1
rec.week_start_day = 0 #0 is for Sunday and so on. WeekStartDay=0
rec.occurrence_count = 0
task.recurrence = True
task.save(dataDir + "AsposeDaily_out.msg", TaskSaveFormat.MSG)
# Set the weekly recurrence
rec1 = MapiCalendarWeeklyRecurrencePattern()
rec1.pattern_type = MapiCalendarRecurrencePatternType.WEEK
rec1.period = 1
rec1.day_of_week = MapiCalendarDayOfWeek.WEDNESDAY
rec1.end_type = MapiCalendarRecurrenceEndType.NEVER_END
rec1.occurrence_count = 0
task.recurrence = rec1
task.save(dataDir + "AsposeWeekly_out.msg", TaskSaveFormat.MSG);
# Set the monthly recurrence
recMonthly = MapiCalendarMonthlyRecurrencePattern()
recMonthly.pattern_type = MapiCalendarRecurrencePatternType.MONTH
recMonthly.period = 1
recMonthly.end_type = MapiCalendarRecurrenceEndType.NEVER_END
recMonthly.day = 30
recMonthly.occurrence_count = 0
recMonthly.week_start_day = 0 #0 is for Sunday and so on. WeekStartDay=0
task.recurrence = recMonthly
task.save(dataDir + "AsposeMonthly_out.msg", TaskSaveFormat.MSG);
# Set the yearly recurrence
recYearly = MapiCalendarMonthlyRecurrencePattern()
recYearly.pattern_type = MapiCalendarRecurrencePatternType.MONTH
recYearly.end_type = MapiCalendarRecurrenceEndType.NEVER_END
recYearly.occurrence_count = 10
recYearly.period = 12
task.recurrence = recYearly
task.save(dataDir + "AsposeYearly_out.msg", TaskSaveFormat.MSG)

Converting a Task to MHT

The following code sample demonstrates how to convert a task to MHT format specifying additional options for the MHT format when task-specific fields should be rendered (RENDER_TASK_FIELDS) and that the header information should be included (WRITE_HEADER). The mht_format_options property of the MhtSaveOptions class is used to define additional options when saving in MHTML format.

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("MapiTask.msg")

opt = ae.SaveOptions.default_mhtml
opt.mht_format_options = ae.MhtFormatOptions.RENDER_TASK_FIELDS | ae.MhtFormatOptions.WRITE_HEADER

msg.save("MapiTask_out.mht", opt)