使用 EWS 创建、管理和删除 Exchange Server 任务

Aspose.Email 支持使用以下方式在 Exchange 上处理任务 ExchangeTask 类。不同属性由 ExchangeTask,比如 Subject, 状态, 截止日期,以及 优先级,可用于在 Exchange 上配置任务。 EWSClient 类公开了如下函数 CreateTask, UpdateTask,以及 DeleteTask 这些用于在 Exchange 上处理任务。本文展示了如何:

  • 创建新任务。
  • 设置任务的时区。
  • 更新任务。
  • 删除任务。
  • 发送任务请求
  • 将任务保存到磁盘

创建任务

以下代码片段展示了如何创建新任务。

// 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);

指定时区

IEWSClient 接口和 ExchangeTask 提供 TimeZoneId 在创建任务时用于设置时区信息的属性。以下代码片段展示了如何指定时区。

client.TimezoneId = "Central Europe Standard Time";

更新任务

下面的代码片段展示了如何在 Exchange 服务器上更新任务。

// 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);
}

删除任务

下面的代码片段演示了如何在 Exchange 服务器上删除任务。

// 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);
    }
}

发送任务请求

Aspose.Email Exchange 服务提供类似 Outlook 的任务请求发送功能。下面的代码片段演示了如何从磁盘加载任务请求消息并使用以下方式发送。 IEWSClient.

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);

将任务保存到磁盘

Aspose.Email 还允许将 Exchange 任务以 Outlook MSG 格式保存到磁盘。下面的代码片段演示了如何将任务保存到磁盘。

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);

列出任务

IEWSClient 提供了 ListTasks 可用于从 Exchange Web Service 获取任务的方法。它有多个重载,可用于从特定文件夹或使用搜索条件检索任务列表。下面的代码示例演示了如何从 Tasks 文件夹获取所有任务或特定任务。

// 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);
}

筛选任务

Aspose.Email 提供从服务器检索特定任务的功能,而不是检索所有任务。该 API 可按任务状态(如已完成、已延期、进行中、未开始或等待他人)检索任务。 ExchangeQueryBuilder 该类可使用 Status 属性指定所需的条件。它还允许指定多个条件以从 Exchange 服务器检索所需的任务。以下代码示例演示了此用法。