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 disk. To create and save a task to disk:
- Instantiate a new object of the MapiContact 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.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// 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); |
Reading a MapiTask
The MapiContact class object is used to cast the MapiMessage object that loads a task from the disk as MSG format. The following code snippet shows you how to read a MapiTask.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
MapiMessage msg = MapiMessage.FromFile(dataDir + "Contact.msg"); | |
MapiContact mapiContact = (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 read a VToDo Task.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
MapiTask task = MapiTask.FromVTodo(dataDir + "VToDoTask.ics"); | |
task.Save(dataDir + "VToDo_out.msg", 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 add reminder information to a MapiTask.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// 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", 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/aspose-email/Aspose.Email-for-.NET | |
// 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 attacment", "Test body of task with attacment", DateTime.Now, DateTime.Now.AddHours(1)); | |
testTask.Attachments.Add(attachmentName, Encoding.Unicode.GetBytes(attachmentContent)); | |
testTask.Save(dataDir + "AddAttachmentsToMapiTask_out", TaskSaveFormat.Msg); |
Adding Recurrence to MapiTask
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.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
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 weekly 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); |
Converting Task to MHT
Aspose.Email can generate MailMessage like output during the conversion of a MapiTask to MHT.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
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); |