Crea, gestisci ed elimina le attività di Exchange Server con EWS
Aspose.Email supporta l’elaborazione di attività su Exchange utilizzando il ExchangeTask classe. Diverse proprietà esposte da ExchangeTask, ad esempio Subject, Stato, Data di scadenza, e Priorità, può essere usata per configurare il task su Exchange. Il EWSClient la classe espone funzioni come CreateTask, UpdateTask, e DeleteTask che sono usati per processare i task su Exchange. Questo articolo mostra come:
- Crea un nuovo task.
- Imposta il fuso orario di un task.
- Aggiorna un task.
- Elimina un task.
- Invia richiesta di task
- Salva task su disco
Crea attività
Il seguente frammento di codice mostra come creare una nuova attività.
// 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);
Specificare il fuso orario
Il IEWSClient interfaccia e ExchangeTask fornire il TimeZoneId proprietà per impostare le informazioni del fuso orario quando si crea un’attività. Il seguente frammento di codice mostra come specificare il fuso orario.
client.TimezoneId = "Central Europe Standard Time";
Aggiorna attività
I seguenti frammenti di codice mostrano come aggiornare un task su un 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);
}
Elimina attività
Il seguente frammento di codice mostra come eliminare un task su un 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);
}
}
Invia richieste di attività
Il servizio Aspose.Email Exchange fornisce la possibilità di inviare richieste di task simili a Outlook. Il seguente frammento di codice mostra come caricare un messaggio di richiesta di task dal disco e inviarlo usando il 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);
Salva attività su disco
Aspose.Email consente anche di salvare i task Exchange su disco in formato Outlook MSG. Il seguente frammento di codice mostra come salvare un task su disco.
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);
Elenca attività
IEWSClient fornisce il ListTasks metodo che può essere usato per recuperare i task da un Exchange Web Service. Ha diversi sovraccarichi che possono essere usati per recuperare l’elenco dei task da una cartella specifica o usando alcuni criteri di ricerca. Il codice di esempio sotto illustra il recupero di tutti o di specifici task dalla cartella 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);
}
Filtra attività
Aspose.Email offre la possibilità di recuperare attività specifiche dal server invece di recuperare tutte le attività dal server. L’API può essere usata per recuperare le attività in base allo stato, come Completate, Differite, In corso, Non avviate o In attesa di altri. Il ExchangeQueryBuilder La classe può essere usata per specificare il criterio desiderato utilizzando la proprietà Status. Consente anche di specificare più condizioni per recuperare i task desiderati dal server Exchange. Questo è dimostrato dal seguente esempio di codice.