Работа с задачами на Exchange Server

Работа с задачами

Aspose.Email поддерживает обработку задач на Exchange Server с помощью класса ExchangeTask. Разные свойства, выставленные ExchangeTask, такие как Subject, Status, DueDate и Priority, могут быть использованы для настройки задачи на Exchange. Класс IEWSClient предоставляет функции, такие как CreateTask, UpdateTask и DeleteTask, которые используются для обработки задач на Exchange Server. Эта статья показывает, как:

  • Создать новую задачу.
  • Установить часовой пояс задачи.
  • Обновить задачу.
  • Удалить задачу.
  • Отправить запрос задачи.
  • Сохранить задачу на диск.

Создать новую задачу

Следующий фрагмент кода показывает, как создать новую задачу.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of EWSClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// Create Exchange task object
System::SharedPtr<ExchangeTask> task = System::MakeObject<ExchangeTask>();
// Set task subject and status to In progress
task->set_Subject(u"New-Test");
task->set_Status(Aspose::Email::Clients::Exchange::WebService::ExchangeTaskStatus::InProgress);
client->CreateTask(client->get_MailboxInfo()->get_TasksUri(), task);

Указание часового пояса

Интерфейс IEWSClient и ExchangeTask предоставляют свойство TimeZoneId для установки информации о часовом поясе при создании задачи. Следующий фрагмент кода показывает, как указать часовой пояс.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
client->set_TimezoneId(u"Central Europe Standard Time");

Обновить задачу

Следующие фрагменты кода показывают, как обновить задачу на сервере Exchange.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of ExchangeClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// Get all tasks info collection from exchange
System::SharedPtr<ExchangeMessageInfoCollection> tasks = client->ListMessages(client->get_MailboxInfo()->get_TasksUri());
// Parse all the tasks info in the list
for (auto info : System::IterateOver(tasks))
{
// Fetch task from exchange using current task info
System::SharedPtr<ExchangeTask> task = client->FetchTask(info->get_UniqueUri());
// Update the task status to NotStarted
task->set_Status(Aspose::Email::Clients::Exchange::WebService::ExchangeTaskStatus::NotStarted);
// Set the task due date
task->set_DueDate(System::DateTime(2013, 2, 26));
// Set task priority
task->set_Priority(Aspose::Email::MailPriority::Low);
// Update task on exchange
client->UpdateTask(task);
}

Удалить задачу

Следующий фрагмент кода показывает, как удалить задачу на сервере Exchange.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of ExchangeClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// Get all tasks info collection from exchange
System::SharedPtr<ExchangeMessageInfoCollection> tasks = client->ListMessages(client->get_MailboxInfo()->get_TasksUri());
// Parse all the tasks info in the list
for (auto info : System::IterateOver(tasks))
{
// Fetch task from exchange using current task info
System::SharedPtr<ExchangeTask> task = client->FetchTask(info->get_UniqueUri());
// Check if the current task fulfills the search criteria
if (System::ObjectExt::Equals(task->get_Subject(), u"test"))
{
//Delete task from exchange
client->DeleteTask(task->get_UniqueUri(), Aspose::Email::Clients::Exchange::WebService::DeleteTaskOptions::DeletePermanently);
}
}

Отправка запроса задачи

Сервис Exchange Aspose.Email предоставляет возможность отправки запросов задач, аналогичных Outlook. Следующий фрагмент кода показывает, как загрузить сообщение о запросе задачи с диска и отправить его с помощью IEWSClient.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::String dataDir = GetDataDir_Exchange();
// Create instance of ExchangeClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::SharedPtr<MsgLoadOptions> options = System::MakeObject<MsgLoadOptions>();
options->set_PreserveTnefAttachments(true);
// load task from .msg file
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + u"task.msg", options);
eml->set_From(MailAddress::to_MailAddress(u"firstname.lastname@domain.com"));
eml->get_To()->Clear();
eml->get_To()->Add(System::MakeObject<MailAddress>(u"firstname.lastname@domain.com"));
client->Send(eml);

Сохранение задачи на диск

Aspose.Email также позволяет сохранять задачи Exchange на диск в формате MSG Outlook. Следующий фрагмент кода показывает, как сохранить задачу на диск.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<ExchangeTask> task = System::MakeObject<ExchangeTask>();
task->set_Subject(System::String(u"TASK-ID - ") + System::Guid::NewGuid());
task->set_Status(Aspose::Email::Clients::Exchange::WebService::ExchangeTaskStatus::InProgress);
task->set_StartDate(System::DateTime::get_Now());
task->set_DueDate(task->get_StartDate().AddDays(3));
System::StaticCast<Calendar::Task>(task)->Save(dstEmail);