Gestire i contatti con Exchange Web Services (EWS)
Aspose.Email ti consente di lavorare con più di semplici messaggi email su Microsoft Exchange Server. Utilizzando il EWSClient API, è possibile recuperare, risolvere, creare, aggiornare ed eliminare i contatti memorizzati in una casella di posta Exchange. Questo articolo spiega le operazioni chiave per la gestione dei contatti con Exchange Web Services (EWS).
Il client rappresenta un contatto come un Contact oggetto. I campioni in questo articolo usano queste intestazioni e spazi dei nomi:
#include <ObjectIdentifier.h>
#include <PersonalInfo/Contact.h>
#include <PersonalInfo/Gender.h>
#include <PersonalInfo/TextFormat.h>
#include <PersonalInfo/EmailAddress.h>
#include <PersonalInfo/EmailAddressList.h>
#include <PersonalInfo/EmailAddressCategory.h>
#include <PersonalInfo/PhoneNumber.h>
#include <PersonalInfo/PhoneNumberList.h>
#include <PersonalInfo/PhoneNumberCategory.h>
#include <PersonalInfo/AssociatedPerson.h>
#include <PersonalInfo/AssociatedPersonsList.h>
#include <PersonalInfo/AssociatedPersonCategory.h>
#include <PersonalInfo/Url.h>
#include <PersonalInfo/URLList.h>
#include <PersonalInfo/UrlCategory.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/ExchangeListContactsOptions.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <system/array.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::PersonalInfo;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
Ottieni Contatti con EWS
GetContacts legge tutti i contatti di una cartella; l’URI della cartella Contatti predefinita proviene dalle informazioni della casella di posta. L’esempio seguente mostra il nome e il primo indirizzo e‑mail di ogni contatto.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
for (SharedPtr<Contact> contact : contacts)
{
SharedPtr<EmailAddress> email = contact->get_EmailAddresses()->get_Email1();
Console::WriteLine(u"Name: " + contact->get_DisplayName() + u", Email Address: " + (email != nullptr ? email->ToString() : String(u"-")));
}
client->Dispose();
Risolvi Contatti per Nome
ResolveContacts cerca i contatti e la directory per voci che corrispondono a un nome, come Outlook risolve un nome digitato nel campo A. ExchangeListContactsOptions::FetchPhoto scarica anche le foto dei contatti.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->ResolveContacts(u"Changed Name", ExchangeListContactsOptions::FetchPhoto);
for (SharedPtr<Contact> contact : contacts)
{
Console::WriteLine(u"Name: " + contact->get_DisplayName());
}
client->Dispose();
Determina il formato delle note del contatto
Il Contact->get_NotesFormat la proprietà specifica il formato del testo delle note secondo il TextFormat enumeratore.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
for (SharedPtr<Contact> contact : client->GetContacts(client->get_MailboxInfo()->get_ContactsUri()))
{
Console::WriteLine(contact->get_DisplayName() + String(u": notes are ") + (contact->get_NotesFormat() == TextFormat::Html ? u"HTML" : u"plain text"));
}
client->Dispose();
Recuperare contatto usando ID
Ogni contatto memorizzato sul server ha un ID - get_Id lo restituisce, e CreateContact restituisce l’ID di uno nuovo. GetContact legge un singolo contatto per quell’ID.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
String id = contacts[0]->get_Id()->get_EWSId();
SharedPtr<Contact> fetchedContact = client->GetContact(id);
Console::WriteLine(u"Fetched: " + fetchedContact->get_DisplayName());
client->Dispose();
Aggiungi contatti
Usa il CreateContact() metodo per aggiungere un nuovo contatto a Exchange Server. Numeri di telefono, persone correlate, indirizzi web e indirizzi e‑mail sono mantenuti in elenchi nel contatto; ogni voce ha una categoria - telefono di casa, coniuge, blog, primo indirizzo e‑mail ecc. - presa dalla corrispondente ...Category classe.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<Contact> contact = MakeObject<Contact>();
contact->set_Gender(Gender::Male);
contact->set_DisplayName(u"Frank Lin");
contact->set_CompanyName(u"ABC Co.");
contact->set_JobTitle(u"Executive Manager");
SharedPtr<PhoneNumber> phone = MakeObject<PhoneNumber>();
phone->set_Number(u"123456789");
phone->set_Category(PhoneNumberCategory::get_Home());
contact->get_PhoneNumbers()->Add(phone);
SharedPtr<AssociatedPerson> spouse = MakeObject<AssociatedPerson>();
spouse->set_Name(u"Catherine");
spouse->set_Category(AssociatedPersonCategory::get_Spouse());
contact->get_AssociatedPersons()->Add(spouse);
SharedPtr<Url> blog = MakeObject<Url>();
blog->set_Href(u"www.blog.com");
blog->set_Category(UrlCategory::get_Blog());
contact->get_Urls()->Add(blog);
SharedPtr<EmailAddress> email = MakeObject<EmailAddress>(u"Frank.Lin@Abc.com", String(u"Frank Lin"));
email->set_Category(EmailAddressCategory::get_Email1());
contact->get_EmailAddresses()->Add(email);
try
{
String id = client->CreateContact(contact);
Console::WriteLine(u"Contact created: " + id);
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
Aggiorna contatti
Le informazioni di contatto possono essere modificate sul server utilizzando IEWSClient->UpdateContact: modifica le proprietà di un contatto letto dal server e lo restituisce. Il seguente esempio di codice rinomina il primo contatto della cartella Contatti.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
SharedPtr<Contact> contact = contacts[0];
Console::WriteLine(u"Name: " + contact->get_DisplayName());
contact->set_DisplayName(u"David Ch");
client->UpdateContact(contact);
client->Dispose();
Eliminazione dei contatti
Un contatto viene eliminato come qualsiasi altro elemento della casella di posta: EliminaElemento ne prende l’ID e il DeletionOptions - get_DeletePermanently() rimuove l’elemento, get_MoveToDeletedItems() lo sposta nella cartella Elementi Eliminati.
Il seguente frammento di codice elimina i contatti con un determinato nome visualizzato.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
String contactToDelete = u"John Teddy";
for (SharedPtr<Contact> contact : client->GetContacts(client->get_MailboxInfo()->get_ContactsUri()))
{
if (contact->get_DisplayName() == contactToDelete)
{
client->DeleteItem(contact->get_Id()->get_EWSId(), DeletionOptions::get_DeletePermanently());
}
}
client->Dispose();