Trabalhando com Tarefas no Exchange Server

Trabalhando com Tarefas

Aspose.Email suporta o processamento de tarefas no Exchange usando a classe ExchangeTask. Diferentes propriedades expostas pela ExchangeTask, como Subject, Status, DueDate e Priority, podem ser usadas para configurar a tarefa no Exchange. A classe EWSClient expõe funções como CreateTask, UpdateTask, e DeleteTask que são usadas para processar tarefas no Exchange. Este artigo mostra como:

  • Criar uma nova tarefa.
  • Definir o fuso horário de uma tarefa.
  • Atualizar uma tarefa.
  • Excluir uma tarefa.
  • Enviar solicitação de tarefa.
  • Salvar tarefa no disco.

Criar Nova Tarefa

O seguinte trecho de código mostra como criar uma nova tarefa.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Create Exchange task object
ExchangeTask task = new ExchangeTask();
// Set task subject and status to In progress
task.Subject = "New-Test";
task.Status = ExchangeTaskStatus.InProgress;
client.CreateTask(client.MailboxInfo.TasksUri, task);

Especificando o Fuso Horário

A interface IEWSClient e ExchangeTask fornecem a propriedade TimeZoneId para definir informações de fuso horário ao criar uma tarefa. O seguinte trecho de código mostra como especificar o fuso horário.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
client.TimezoneId = "Central Europe Standard Time";

Atualizar Tarefa

Os seguintes trechos de código mostram como atualizar uma tarefa em um servidor Exchange.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create and initialize credentials
var credentials = new NetworkCredential("username", "12345");
// Create instance of ExchangeClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get all tasks info collection from exchange
ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri);
// Parse all the tasks info in the list
foreach (ExchangeMessageInfo info in tasks)
{
// Fetch task from exchange using current task info
ExchangeTask task = client.FetchTask(info.UniqueUri);
// Update the task status to NotStarted
task.Status = ExchangeTaskStatus.NotStarted;
// Set the task due date
task.DueDate = new DateTime(2013, 2, 26);
// Set task priority
task.Priority = MailPriority.Low;
// Update task on exchange
client.UpdateTask(task);
}

Excluir Tarefa

O seguinte trecho de código mostra como excluir uma tarefa em um servidor Exchange.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of ExchangeClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get all tasks info collection from exchange
ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri);
// Parse all the tasks info in the list
foreach (ExchangeMessageInfo info in tasks)
{
// Fetch task from exchange using current task info
ExchangeTask task = client.FetchTask(info.UniqueUri);
// Check if the current task fulfills the search criteria
if (task.Subject.Equals("test"))
{
//Delete task from exchange
client.DeleteItem(task.UniqueUri, DeletionOptions.DeletePermanently);
}
}

Enviando Solicitação de Tarefa

O serviço Exchange do Aspose.Email fornece a capacidade de enviar solicitações de tarefa semelhantes ao Outlook. O seguinte trecho de código mostra como carregar uma mensagem de solicitação de tarefa do disco e enviá-la usando o IEWSClient.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_Exchange();
// Create instance of ExchangeClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
MsgLoadOptions options = new MsgLoadOptions();
options.PreserveTnefAttachments = true;
// load task from .msg file
MailMessage eml = MailMessage.Load(dataDir + "task.msg", options);
eml.From = "firstname.lastname@domain.com";
eml.To.Clear();
eml.To.Add(new MailAddress("firstname.lastname@domain.com"));
client.Send(eml);

Salvando Tarefa no Disco

Aspose.Email também permite salvar Tarefas do Exchange no disco no formato MSG do Outlook. O seguinte trecho de código mostra como salvar uma tarefa no disco.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
ExchangeTask task = new ExchangeTask();
task.Subject = "TASK-ID - " + Guid.NewGuid();
task.Status = ExchangeTaskStatus.InProgress;
task.StartDate = DateTime.Now;
task.DueDate = task.StartDate.AddDays(3);
task.Save(dstEmail);

Listando Tarefas do Servidor Exchange

O IEWSClient fornece o método ListTasks que pode ser usado para buscar tarefas de um Serviço Web do Exchange. Possui várias sobrecargas que podem ser usadas para recuperar a lista de tarefas de uma pasta específica ou usando algum critério de pesquisa. O exemplo de código abaixo ilustra como obter todas ou tarefas específicas da pasta de Tarefas.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Set mailboxURI, Username, password, domain information
string mailboxUri = "https://ex2010/ews/exchange.asmx";
string username = "test.exchange";
string password = "pwd";
string domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);
//Listing Tasks from Server
client.TimezoneId = "Central Europe Standard Time";
TaskCollection taskCollection = client.ListTasks(client.MailboxInfo.TasksUri);
//print retrieved tasks' details
foreach (ExchangeTask task in taskCollection)
{
Console.WriteLine(client.TimezoneId);
Console.WriteLine(task.Subject);
Console.WriteLine(task.StartDate);
Console.WriteLine(task.DueDate);
}
//Listing Tasks from server based on Query - Completed and In-Progress
ExchangeQueryBuilder builder = new ExchangeQueryBuilder();
ExchangeTaskStatus[] selectedStatuses = new ExchangeTaskStatus[]
{
ExchangeTaskStatus.Completed,
ExchangeTaskStatus.InProgress
};
builder.TaskStatus.In(selectedStatuses);
MailQuery query = builder.GetQuery();
taskCollection = client.ListTasks(client.MailboxInfo.TasksUri, query);
//print retrieved tasks' details
foreach (ExchangeTask task in taskCollection)
{
Console.WriteLine(client.TimezoneId);
Console.WriteLine(task.Subject);
Console.WriteLine(task.StartDate);
Console.WriteLine(task.DueDate);
}

Filtrando Tarefas do Servidor Exchange

Aspose.Email fornece a capacidade de recuperar tarefas específicas do servidor em vez de recuperar todas as tarefas do servidor. A API pode ser usada para recuperar tarefas por status da tarefa, como Concluída, Adiada, Em Progresso, Não iniciada ou Aguardando outros. A classe ExchangeQueryBuilder pode ser usada para especificar o critério desejado utilizando a propriedade Status. Ela também permite especificar várias condições para recuperar as tarefas desejadas do Servidor Exchange. Isso é demonstrado pelo seguinte exemplo de código.