สร้าง, จัดการ และลบงานของ Exchange Server ด้วย EWS

Aspose.Email รองรับการประมวลผลงานบน Exchange โดยใช้ ExchangeTask class. คุณสมบัติต่าง ๆ ที่เปิดเผยโดย ExchangeTask, เช่น Subject, สถานะ, วันครบกำหนด, และ ระดับความสำคัญ, สามารถใช้เพื่อกำหนดค่าการทำงานบน Exchange. The 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 คุณสมบัติสำหรับกำหนดข้อมูลโซนเวลาเมื่อสร้างงาน โค้ดสคริปต์ต่อไปนี้แสดงวิธีการระบุ Timezone.

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 สามารถใช้ดึงงานตามสถานะของงาน เช่น Completed, Deferred, In Progress, Not started หรือ Waiting on others. The ExchangeQueryBuilder คลาสนี้สามารถใช้ระบุเกณฑ์ที่ต้องการโดยใช้คุณสมบัติ Status. นอกจากนี้ยังอนุญาตให้ระบุหลายเงื่อนไขเพื่อดึงงานที่ต้องการจาก Exchange Server. ตัวอย่างโค้ดต่อไปนี้แสดงการทำเช่นนั้น.