Working with Outlook Tasks
Creating, Saving and Reading Outlook Tasks
Aspose.Email for Java allows developers to create Outlook tasks and save them as MSG format. The MapiTask class provides a number of properties such as PercentComplete, EstimatedEffort, ActualEffort, History, LastUpdate, etc., to accommodate and set information required for an Outlook task. This article shows how to create, save and read a MapiTask from disk.
Create and Save a MapiTask
The following steps can be used to create and save a Task to disk.
- Instantiate a new object of the MapiTask class.
- Enter information for the task various properties.
- Save the task to disk in MSG format.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.DECEMBER, 1, 0, 0, 0);
Date dueDate = calendar.getTime();
MapiTask task = new MapiTask("To Do", "Just click and type to add a new task", startDate, dueDate);
task.setPercentComplete(20);
task.setEstimatedEffort(2000);
task.setActualEffort(20);
task.setHistory(MapiTaskHistory.Assigned);
task.getUsers().setOwner("Darius");
task.getUsers().setLastAssigner("Harkness");
task.getUsers().setLastDelegate("Harkness");
task.getUsers().setDelegator("Test Delegator");
task.getUsers().setOwnership(MapiTaskOwnership.AssignersCopy);
task.setCompanies(new String[] { "company1", "company2", "company3" });
task.setCategories(new String[] { "category1", "category2", "category3" });
task.setMileage("Some test mileage");
task.setBilling("Test billing information");
task.setSensitivity(MapiSensitivity.Personal);
task.setStatus(MapiTaskStatus.Complete);
task.save(dataDir + "MapiTask_out.msg", TaskSaveFormat.Msg);
Read a MapiTask
The MapiTask class object is used to cast the MapiMessage object that loads a task from the disk in MSG format.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "MapiTask.msg");
MapiTask task = (MapiTask) msg.toMapiMessageItem();
System.out.println("Subject: " + task.getSubject());
System.out.println("Status: " + task.getStatus());
Read 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.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MapiTask task = MapiTask.fromVTodo(dataDir + "sample.ics");
task.save(dataDir + "Test_out.msg", TaskSaveFormat.Msg);
Add 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-Java
String dataDir = "data/";
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date date = calendar.getTime();
MapiTask task = new MapiTask("task with reminder", "this is a body", date, date);
task.setReminderSet(true);
task.setReminderTime(date);
task.setReminderFileParameter(dataDir + "Alarm01.wav");
task.save(dataDir + "OutputTask_out.msg", TaskSaveFormat.Msg);
Add Attachment 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-Java
String dataDir = "data/";
MapiTask task = new MapiTask("To Do", "Just click and type to add new tasks", new Date(), new Date());
try {
byte[] content = Files.readAllBytes(Paths.get(dataDir + "Attach.txt"));
task.getAttachments().add("TestAtt.txt", content);
} catch (IOException e) {
e.printStackTrace();
}
task.save(dataDir + "MapiTask_out.msg", TaskSaveFormat.Msg);
Add Recurrence to a 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:
Date startDate = new GregorianCalendar(2015, java.util.Calendar.APRIL, 30, 10, 0, 0).getTime();
Date endDate = new GregorianCalendar(2015, java.util.Calendar.APRIL, 30, 11, 0, 0).getTime();
MapiTask task = new MapiTask("abc", "def", startDate, endDate);
task.setState(MapiTaskState.NotAssigned);
// Set a daily recurrence
MapiCalendarDailyRecurrencePattern daily = new MapiCalendarDailyRecurrencePattern();
daily.setPatternType(MapiCalendarRecurrencePatternType.Day);
daily.setPeriod(1);
daily.setWeekStartDay(DayOfWeek.Sunday);
daily.setEndType(MapiCalendarRecurrenceEndType.NeverEnd);
daily.setOccurrenceCount(0);
task.setRecurrence(daily);
task.save("AsposeDaily_out.msg", TaskSaveFormat.Msg);
// Set a weekly recurrence
MapiCalendarWeeklyRecurrencePattern weekly = new MapiCalendarWeeklyRecurrencePattern();
weekly.setPatternType(MapiCalendarRecurrencePatternType.Week);
weekly.setPeriod(1);
weekly.setDayOfWeek(MapiCalendarDayOfWeek.Wednesday);
weekly.setEndType(MapiCalendarRecurrenceEndType.NeverEnd);
weekly.setOccurrenceCount(0);
task.setRecurrence(weekly);
task.save("AsposeWeekly_out.msg", TaskSaveFormat.Msg);
// Set a monthly recurrence
MapiCalendarMonthlyRecurrencePattern monthly = new MapiCalendarMonthlyRecurrencePattern();
monthly.setPatternType(MapiCalendarRecurrencePatternType.Month);
monthly.setPeriod(1);
monthly.setDay(30);
monthly.setEndType(MapiCalendarRecurrenceEndType.NeverEnd);
monthly.setOccurrenceCount(0);
monthly.setWeekStartDay(DayOfWeek.Sunday);
task.setRecurrence(monthly);
task.save("AsposeMonthly_out.msg", TaskSaveFormat.Msg);
// Set a yearly recurrence - a monthly pattern repeated every 12 months
MapiCalendarMonthlyRecurrencePattern yearly = new MapiCalendarMonthlyRecurrencePattern();
yearly.setPatternType(MapiCalendarRecurrencePatternType.Month);
yearly.setPeriod(12);
yearly.setEndType(MapiCalendarRecurrenceEndType.NeverEnd);
yearly.setOccurrenceCount(10);
task.setRecurrence(yearly);
task.save("AsposeYearly_out.msg", TaskSaveFormat.Msg);
Converting Outlook Task to MHT
Aspose.Email can generate MapiMessage 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-Java
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "MapiTask.msg");
MhtSaveOptions opt = SaveOptions.getDefaultMhtml();
opt.setMhtFormatOptions(MhtFormatOptions.RenderTaskFields | MhtFormatOptions.WriteHeader);
// The task templates live in the nested MhtTemplateName.Task class
opt.getFormatTemplates().clear();
opt.getFormatTemplates().add(MhtTemplateName.Task.SUBJECT,
"<span class='headerLineTitle'>Subject:</span><span class='headerLineText'>{0}</span><br/>");
opt.getFormatTemplates().add(MhtTemplateName.Task.ACTUAL_WORK,
"<span class='headerLineTitle'>Actual Work:</span><span class='headerLineText'>{0}</span><br/>");
opt.getFormatTemplates().add(MhtTemplateName.Task.TOTAL_WORK,
"<span class='headerLineTitle'>Total Work:</span><span class='headerLineText'>{0}</span><br/>");
opt.getFormatTemplates().add(MhtTemplateName.Task.STATUS,
"<span class='headerLineTitle'>Status:</span><span class='headerLineText'>{0}</span><br/>");
opt.getFormatTemplates().add(MhtTemplateName.Task.OWNER,
"<span class='headerLineTitle'>Owner:</span><span class='headerLineText'>{0}</span><br/>");
opt.getFormatTemplates().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 value 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 the header when saving an HTML file:
MapiMessage msg = MapiMessage.load("task.msg");
HtmlSaveOptions opt = SaveOptions.getDefaultHtml();
opt.setHtmlFormatOptions(HtmlFormatOptions.WriteHeader | HtmlFormatOptions.RenderTaskFields);
msg.save("task.html", opt);