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.

  1. Instantiate a new object of the MapiTask class.
  2. Enter information for the task various properties.
  3. 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
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2016, Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2016, Calendar.DECEMBER, 1);
Date endDate = calendar.getTime();
MapiTask task = new MapiTask("To Do", "Just click and type to add new task", startDate, endDate);
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().setOwnership(MapiTaskOwnership.AssignersCopy);
String[] companies = { "company1", "company2", "company3" };
task.setCompanies(companies);
String[] categories = { "category1", "category2", "category3" };
task.setCategories(categories);
task.setMileage("Some test mileage");
task.setBilling("Test billing information");
task.getUsers().setDelegator("Test Delegator");
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
MapiMessage msg = MapiMessage.fromFile(dataDir + "MapiTask_out.msg");
MapiTask task2 = (MapiTask) msg.toMapiMessageItem();

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
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
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2012, Calendar.NOVEMBER, 1, 0, 0, 0);
Date date = calendar.getTime();
MapiTask testTask = new MapiTask("task with reminder", "this is a body", date, date);
testTask.setReminderSet(true);
testTask.setReminderTime(date);
testTask.setReminderFileParameter(dataDir + "Alarm01.wav");
testTask.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
MapiTask task = new MapiTask("To Do", "Just click and type to add new tasks", new Date(), new Date());
Path p = FileSystems.getDefault().getPath("", dataDir + "Attach.txt");
try
{
task.getAttachments().add(dataDir + "TestAtt.txt", Files.readAllBytes(p));
}
catch (IOException e)
{
e.printStackTrace();
}
task.save(dataDir + "MapiTask_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 = Utils.getSharedDataDir(CreateAndSaveOutlookMessageFile.class) + "outlook/";
MapiMessage msg = MapiMessage.fromFile(dataDir + "MapiTask.msg");
MhtSaveOptions opt = SaveOptions.getDefaultMhtml();
opt.setMhtFormatOptions(MhtFormatOptions.RenderTaskFields | MhtFormatOptions.WriteHeader);
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);