Buat, Kelola, dan Hapus Tugas Server Exchange dengan EWS
Aspose.Email mendukung pemrosesan tugas di Exchange menggunakan ExchangeTask kelas. Berbagai properti yang diekspos oleh ExchangeTask, seperti Subject, Status, Tanggal Jatuh Tempo, dan Prioritas, dapat digunakan untuk mengonfigurasi tugas pada Exchange. The EWSClient kelas mengekspos fungsi seperti CreateTask, UpdateTask, dan DeleteTask yang digunakan untuk memproses tugas pada Exchange. Artikel ini menunjukkan cara:
- Buat tugas baru.
- Atur zona waktu tugas.
- Perbarui sebuah tugas.
- Hapus sebuah tugas.
- Kirim Permintaan Tugas
- Simpan Tugas ke Disk
Buat Tugas
Potongan kode berikut menunjukkan cara membuat tugas baru.
// 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);
Tentukan Zona Waktu
The IEWSClient antarmuka dan ExchangeTask menyediakan TimeZoneId properti untuk mengatur informasi zona waktu saat membuat tugas. Potongan kode berikut menunjukkan cara menentukan Zona Waktu.
client.TimezoneId = "Central Europe Standard Time";
Perbarui Tugas
Potongan kode berikut menunjukkan cara memperbarui tugas pada server 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);
}
Hapus Tugas
Potongan kode berikut menunjukkan cara menghapus tugas pada server 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);
}
}
Kirim Permintaan Tugas
Layanan Aspose.Email Exchange menyediakan kemampuan untuk mengirim permintaan tugas mirip Outlook. Potongan kode berikut menunjukkan cara memuat pesan permintaan tugas dari disk dan mengirimnya menggunakan 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);
Simpan Tugas ke Disk
Aspose.Email juga memungkinkan penyimpanan Tugas Exchange ke disk dalam format Outlook MSG. Potongan kode berikut menunjukkan cara menyimpan tugas ke disk.
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);
Daftar Tugas
IEWSClient menyediakan ListTasks metode yang dapat digunakan untuk mengambil tugas dari Exchange Web Service. Ia memiliki beberapa overload yang dapat digunakan untuk mengambil daftar tugas dari folder tertentu atau dengan beberapa kriteria pencarian. Contoh kode di bawah mengilustrasikan pengambilan semua atau tugas tertentu dari folder 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);
}
Filter Tugas
Aspose.Email menyediakan kemampuan untuk mengambil tugas spesifik dari server alih-alih mengambil semua tugas dari server. API dapat digunakan untuk mengambil tugas berdasarkan status tugas seperti Selesai, Ditunda, Sedang Berlangsung, Belum Dimulai, atau Menunggu orang lain. ExchangeQueryBuilder kelas dapat digunakan untuk menentukan kriteria yang diinginkan dengan memanfaatkan properti Status. Ini juga memungkinkan penentuan beberapa kondisi untuk mengambil tugas yang diinginkan dari Server Exchange. Hal ini ditunjukkan oleh contoh kode berikut.