Create, Save and Read Outlook Tasks
This article covers standalone Outlook MapiTask items saved as MSG. To store and read tasks inside a PST, see Managing Tasks, Journals, and Notes in PST Files.
Create Outlook 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 disk. To create and save a task to disk:
- Instantiate a new object of the MapiTask class.
- Enter task property information.
- Save the task to disc in MSG format.
The following code snippet shows you how to create, save and read Tasks.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
MapiTask task = new MapiTask("To Do", "Just click and type to add new task", DateTime.Now, DateTime.Now.AddDays(3));
task.PercentComplete = 20;
task.EstimatedEffort = 2000;
task.ActualEffort = 20;
task.History = MapiTaskHistory.Assigned;
task.LastUpdate = DateTime.Now;
task.Users.Owner = "Darius";
task.Users.LastAssigner = "Harkness";
task.Users.LastDelegate = "Harkness";
task.Users.Ownership = MapiTaskOwnership.AssignersCopy;
task.Companies = new string[] { "company1", "company2", "company3" };
task.Categories = new string[] { "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.EstimatedEffort = 5;
task.Save(dataDir + "MapiTask.msg", TaskSaveFormat.Msg);
Read MAPI Tasks
Tasks saved in MSG format can be loaded into MapiMessage and then converted to MapiTask with access to their specific properties, such as the subject and status. The following code sample demonstrates how to read and manipulate task details from saved MSG files:
MapiMessage msg = MapiMessage.FromFile(dataDir + "MapiTask.msg");
MapiTask task = (MapiTask)msg.ToMapiMessageItem();
Console.WriteLine("Subject: " + task.Subject);
Console.WriteLine("Status: " + task.Status);
Console.WriteLine("Percent complete: " + task.PercentComplete);
Read VToDo Tasks
Outlook 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 read a VToDo Task.
MapiTask task = MapiTask.FromVTodo(dataDir + "VToDoTask.ics");
task.Save(dataDir + "VToDo_out.msg", TaskSaveFormat.Msg);
Add Reminder Information to MAPI Tasks
Similar to Microsoft Outlook, Aspose.Email can add reminder information to a MapiTask. The following code snippet shows you how to add reminder information to a MapiTask.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Create MapiTask and set Task Properties
MapiTask testTask = new MapiTask("task with reminder", "this is a body", DateTime.Now, DateTime.Now.AddHours(1));
testTask.ReminderSet = true;
testTask.ReminderTime = DateTime.Now;
testTask.ReminderFileParameter =dataDir + "Alarm01.wav";
testTask.Save(dataDir + "AddReminderInformationToMapiTask_out.msg", TaskSaveFormat.Msg);
Add Attachments to MAPI Tasks
The following code snippet shows you how to add attachments to a MapiTask.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
string attachmentContent = "Test attachment body";
string attachmentName = "Test attachment name";
MapiTask testTask = new MapiTask("Task with attachment", "Test body of task with attachment", DateTime.Now, DateTime.Now.AddHours(1));
testTask.Attachments.Add(attachmentName, Encoding.Unicode.GetBytes(attachmentContent));
testTask.Save(dataDir + "AddAttachmentsToMapiTask_out.msg", TaskSaveFormat.Msg);
Add Recurrence to MAPI Tasks
Aspose.Email allows creating a recurring task where the recurrence can be daily, weekly, monthly, or yearly. The following code snippet shows you how to create a task with different recurrence types.
DateTime startDate = new DateTime(2015, 04, 30, 10, 00, 00);
MapiTask task = new MapiTask("abc", "def", startDate, startDate.AddHours(1));
task.State = MapiTaskState.NotAssigned;
// Set the daily recurrence
var rec = new MapiCalendarDailyRecurrencePattern
{
PatternType = MapiCalendarRecurrencePatternType.Day,
Period = 1,
WeekStartDay = DayOfWeek.Sunday,
EndType = MapiCalendarRecurrenceEndType.NeverEnd,
OccurrenceCount = 0,
};
task.Recurrence = rec;
task.Save(dataDir + "AsposeDaily_out.msg", TaskSaveFormat.Msg);
// Set the weekly recurrence
var rec1 = new MapiCalendarWeeklyRecurrencePattern
{
PatternType = MapiCalendarRecurrencePatternType.Week,
Period = 1,
DayOfWeek = MapiCalendarDayOfWeek.Wednesday,
EndType = MapiCalendarRecurrenceEndType.NeverEnd,
OccurrenceCount = 0,
};
task.Recurrence = rec1;
task.Save(dataDir + "AsposeWeekly_out.msg", TaskSaveFormat.Msg);
// Set the monthly recurrence
var recMonthly = new MapiCalendarMonthlyRecurrencePattern
{
PatternType = MapiCalendarRecurrencePatternType.Month,
Period = 1,
EndType = MapiCalendarRecurrenceEndType.NeverEnd,
Day = 30,
OccurrenceCount = 0,
WeekStartDay = DayOfWeek.Sunday,
};
task.Recurrence = recMonthly;
//task.Save(dataDir + "AsposeMonthly_out.msg", TaskSaveFormat.Msg);
// Set the yearly recurrence
var recYearly = new MapiCalendarMonthlyRecurrencePattern
{
PatternType = MapiCalendarRecurrencePatternType.Month,
EndType = MapiCalendarRecurrenceEndType.NeverEnd,
OccurrenceCount = 10,
Period = 12,
};
task.Recurrence = recYearly;
//task.Save(dataDir + "AsposeYearly_out.msg", TaskSaveFormat.Msg);
Convert Tasks to MHT
Aspose.Email can generate MailMessage like output during the conversion of a MapiTask to MHT.
MapiMessage msg = MapiMessage.FromFile(dataDir + "MapiTask.msg");
MhtSaveOptions opt = SaveOptions.DefaultMhtml;
opt.MhtFormatOptions = MhtFormatOptions.RenderTaskFields | MhtFormatOptions.WriteHeader;
opt.FormatTemplates.Clear();
opt.FormatTemplates.Add(MhtTemplateName.Task.Subject, "<span class='headerLineTitle'>Subject:</span><span class='headerLineText'>{0}</span><br/>");
opt.FormatTemplates.Add(MhtTemplateName.Task.ActualWork, "<span class='headerLineTitle'>Actual Work:</span><span class='headerLineText'>{0}</span><br/>");
opt.FormatTemplates.Add(MhtTemplateName.Task.TotalWork, "<span class='headerLineTitle'>Total Work:</span><span class='headerLineText'>{0}</span><br/>");
opt.FormatTemplates.Add(MhtTemplateName.Task.Status, "<span class='headerLineTitle'>Status:</span><span class='headerLineText'>{0}</span><br/>");
opt.FormatTemplates.Add(MhtTemplateName.Task.Owner, "<span class='headerLineTitle'>Owner:</span><span class='headerLineText'>{0}</span><br/>");
opt.FormatTemplates.Add(MhtTemplateName.Task.Priority, "<span class='headerLineTitle'>Priority:</span><span class='headerLineText'>{0}</span><br/>");
msg.Save(dataDir + "MapiTask_out.mht", opt);
MSG to HTML Conversion Preserving Task Fields
The HtmlFormatOptions.RenderTaskFields enumeration allows you to specify that task fields should be included in the header of the saved HTML file. The following code snippet shows how to preserve task fields in a header when saving a html file:
var msg = MapiMessage.Load("task.msg");
HtmlSaveOptions opt = SaveOptions.DefaultHtml;
opt.HtmlFormatOptions = HtmlFormatOptions.WriteHeader | HtmlFormatOptions.RenderTaskFields;
msg.Save("task.html", opt);
See Also
- Managing Tasks, Journals, and Notes in PST Files — store and read tasks, journals, and notes inside a PST.
- Create, Save and Read Outlook Notes — work with standalone
MapiNoteitems.