Working with Tasks on Exchange Server
Working with Tasks
Aspose.Email supports processing tasks on Exchange Server using the ExchangeTask class. Different properties exposed by ExchangeTask, like the subject, the status (ExchangeTaskStatus), the due date and the priority, can be used to configure the task on Exchange. The IEWSClient class exposes methods like CreateTask, FetchTask, UpdateTask and DeleteItem which are used to process tasks on the Exchange Server. This article shows how to:
- Create a new task.
- Set a task’s timezone.
- Update a task.
- Delete a task.
- Send Task Request
- Save Task to Disc
The samples in this article use these headers and namespaces:
#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <MailPriority.h>
#include <MsgLoadOptions.h>
#include <Mapi/TaskSaveFormat.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/DeletionOptions.h>
#include <Clients/Exchange/WebService/Tasks/ExchangeTask.h>
#include <Clients/Exchange/WebService/Tasks/ExchangeTaskStatus.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <Clients/Exchange/ExchangeMessageInfo.h>
#include <Clients/Exchange/ExchangeMessageInfoCollection.h>
#include <system/date_time.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
Create New Task
CreateTask() creates a task in the given folder, here the default Tasks folder of the mailbox, and returns the URI of the new task.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<ExchangeTask> task = MakeObject<ExchangeTask>();
task->set_Subject(u"New-Test");
task->set_Status(ExchangeTaskStatus::InProgress);
String taskUri = client->CreateTask(client->get_MailboxInfo()->get_TasksUri(), task);
Console::WriteLine(u"Task created: " + taskUri);
client->Dispose();
Specifying Timezone
The dates of a task are interpreted in the time zone of the client. set_TimezoneId() of IEWSClient sets this time zone by its Windows ID before the tasks are created or updated.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
client->set_TimezoneId(u"Central Europe Standard Time");
SharedPtr<ExchangeTask> task = MakeObject<ExchangeTask>();
task->set_Subject(u"Task in Central European time");
task->set_StartDate(DateTime::get_Now());
task->set_DueDate(DateTime::get_Now().AddDays(3));
client->CreateTask(client->get_MailboxInfo()->get_TasksUri(), task);
client->Dispose();
Update Task
ListMessages() lists the items of the Tasks folder; FetchTask() reads a task by its URI. The sample changes the status, the due date and the priority of every task and saves the changes with UpdateTask().
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
for (SharedPtr<ExchangeMessageInfo> info : client->ListMessages(client->get_MailboxInfo()->get_TasksUri()))
{
SharedPtr<ExchangeTask> task = client->FetchTask(info->get_UniqueUri());
task->set_Status(ExchangeTaskStatus::NotStarted);
task->set_DueDate(DateTime::get_Now().AddDays(7));
task->set_Priority(MailPriority::Low);
client->UpdateTask(task);
}
client->Dispose();
Delete Task
A task is deleted like any other item of the mailbox: DeleteItem() takes its URI and the DeletionOptions - get_DeletePermanently() removes the task, get_MoveToDeletedItems() moves it to the Deleted Items folder. The sample deletes the tasks with the subject “test”.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
for (SharedPtr<ExchangeMessageInfo> info : client->ListMessages(client->get_MailboxInfo()->get_TasksUri()))
{
SharedPtr<ExchangeTask> task = client->FetchTask(info->get_UniqueUri());
if (task->get_Subject() == u"test")
{
client->DeleteItem(task->get_UniqueUri(), DeletionOptions::get_DeletePermanently());
}
}
client->Dispose();
Sending Task Request
Aspose.Email Exchange service provides the capability to send task requests similar to Outlook. The following code snippet loads a task request message from an MSG file and sends it using the IEWSClient. MsgLoadOptions::set_PreserveTnefAttachments() keeps the task data of the message in its TNEF form, so the recipient receives a task request and not a plain message.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<MsgLoadOptions> options = MakeObject<MsgLoadOptions>();
options->set_PreserveTnefAttachments(true);
SharedPtr<MailMessage> message = MailMessage::Load(u"task.msg", options);
message->set_From(MakeObject<MailAddress>(u"firstname.lastname@domain.com"));
message->get_To()->Clear();
message->get_To()->Add(MakeObject<MailAddress>(u"firstname.lastname@domain.com"));
client->Send(message);
client->Dispose();
Saving Task to Disc
Aspose.Email also allows saving Exchange Tasks to disc in Outlook MSG format: the Save() method of ExchangeTask takes the file name and the format, TaskSaveFormat::Msg. The task does not have to be stored on the server.
SharedPtr<ExchangeTask> task = MakeObject<ExchangeTask>();
task->set_Subject(u"Task to save");
task->set_Status(ExchangeTaskStatus::InProgress);
task->set_StartDate(DateTime::get_Now());
task->set_DueDate(task->get_StartDate().AddDays(3));
task->Save(u"task_out.msg", Mapi::TaskSaveFormat::Msg);