ทำงานกับงานบน Exchange Server
ทำงานกับงาน
Aspose.Email รองรับการประมวลผลงานบน Exchange Server ด้วย ExchangeTask class. คุณสมบัติต่าง ๆ ที่เปิดเผยโดย ExchangeTask, เช่น หัวเรื่อง, สถานะ (ExchangeTaskStatus), วันที่ครบกำหนดและความสำคัญ, สามารถใช้กำหนดค่าการทำงานบน Exchange. The IEWSClient คลาสเปิดเผยเมธอดเช่น CreateTask, FetchTask, UpdateTask และ DeleteItem ซึ่งใช้ในการประมวลผลงานบน Exchange Server. บทความนี้แสดงวิธี:
- สร้างงานใหม่.
- ตั้งค่าโซนเวลาของงาน.
- อัปเดตงาน.
- ลบงาน.
- ส่งคำขอทำงาน
- บันทึกงานลงดิสก์
ตัวอย่างในบทความนี้ใช้ส่วนหัวและเนมสเปซต่อไปนี้:
#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;
สร้างงานใหม่
CreateTask() สร้างงานในโฟลเดอร์ที่ระบุ, ที่นี่คือโฟลเดอร์ Tasks เริ่มต้นของกล่องจดหมาย, และคืนค่า URI ของงานใหม่
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();
ระบุโซนเวลา
วันที่ของงานจะถูกตีความตามโซนเวลาของไคลเอนต์ set_TimezoneId() ของ IEWSClient ตั้งโซนเวลาเหล่านี้ตาม ID ของ Windows ก่อนสร้างหรืออัปเดตงาน
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();
อัปเดตงาน
ListMessages() แสดงรายการของโฟลเดอร์ Tasks; FetchTask() อ่านงานตาม URI ตัวอย่างเปลี่ยนสถานะ, วันครบกำหนดและความสำคัญของทุกงานและบันทึกการเปลี่ยนแปลงด้วย 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();
ลบงาน
งานจะถูกลบเช่นรายการอื่นในกล่องจดหมาย: DeleteItem() รับ URI ของมันและ DeletionOptions - get_DeletePermanently() ลบงาน, get_MoveToDeletedItems() ย้ายไปโฟลเดอร์ Deleted Items ตัวอย่างลบงานที่มีหัวข้อ "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();
การส่งคำขอทำงาน
บริการ Aspose.Email Exchange ให้ความสามารถส่งคำขอ งานคล้าย Outlook โค้ดต่อไปโหลดข้อความคำขอ งานจากไฟล์ MSG และส่งด้วย IEWSClient. MsgLoadOptions::set_PreserveTnefAttachments() เก็บข้อมูลงานของข้อความในรูปแบบ TNEF, ดังนั้นผู้รับจะได้รับคำขอ งาน ไม่ใช่ข้อความธรรมดา
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();
บันทึกงานลงดิสก์
Aspose.Email ยังอนุญาตให้บันทึกงาน Exchange ไปยังดิสก์ในรูปแบบ Outlook MSG: ส่วน Save() เมธอดของ ExchangeTask รับชื่อไฟล์และรูปแบบ, TaskSaveFormat::Msg. งานไม่จำเป็นต้องเก็บบนเซิร์ฟเวอร์
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);