Verwalten von Kontakten mit Exchange Web Services (EWS)

Aspose.Email ermöglicht Ihnen die Arbeit mit mehr als nur E‑Mail‑Nachrichten auf Microsoft Exchange Server. Mit dem EWSClient API können Sie Kontakte, die in einem Exchange‑Postfach gespeichert sind, abrufen, auflösen, erstellen, aktualisieren und löschen. Dieser Artikel erklärt die wichtigsten Vorgänge zum Verwalten von Kontakten mit Exchange Web Services (EWS).

Der Client stellt einen Kontakt als Kontakt Objekt. Die Beispiele in diesem Artikel verwenden diese Header und Namespaces:

#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;

Kontakte mit EWS abrufen

GetContacts liest alle Kontakte eines Ordners; die URI des Standard‑Ordners Kontakte stammt aus den Postfach‑Informationen. Das nachfolgende Beispiel zeigt den Namen und die erste E‑Mail‑Adresse jedes Kontakts.

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();

Kontakte nach Name auflösen

ResolveContacts sucht in den Kontakten und im Verzeichnis nach Einträgen, die zu einem Namen passen, so wie Outlook einen im An‑Feld eingegebenen Namen auflöst. ExchangeListContactsOptions::FetchPhoto lädt außerdem die Kontaktfotos herunter.

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();

Kontaktnotizenformat bestimmen

Die Contact->get_NotesFormat Eigenschaft gibt das Format des Notiztextes gemäß dem TextFormat Enumerator.

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();

Kontakt mit ID abrufen

Jeder im Server gespeicherte Kontakt hat eine ID – get_Id gibt sie zurück und CreateContact gibt die ID eines neuen zurück. GetContact liest einen einzelnen Kontakt anhand dieser 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();

Kontakte hinzufügen

Verwenden Sie die CreateContact() Methode zum Hinzufügen eines neuen Kontakts zu Exchange Server. Telefonnummern, zugehörige Personen, Webadressen und E‑Mail‑Adressen werden in Listen im Kontakt gespeichert; jeder Eintrag hat eine Kategorie – Privat‑Telefon, Partner, Blog, erste E‑Mail‑Adresse usw. – übernommen aus dem passenden ...Category Klasse.

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();

Kontakte aktualisieren

Kontaktinformationen können auf dem Server geändert werden mit IEWSClient->UpdateContact: ändern Sie die Eigenschaften eines vom Server gelesenen Kontakts und geben Sie ihn zurück. Das folgende Codebeispiel benennt den ersten Kontakt im Ordner Kontakte um.

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();

Kontakte löschen

Ein Kontakt wird wie jedes andere Element des Postfachs gelöscht: DeleteItem nimmt dessen ID und die DeletionOptions - get_DeletePermanently() entfernt das Element, get_MoveToDeletedItems() verschiebt sie in den Ordner Gelöschte Elemente.

Das folgende Codefragment löscht die Kontakte mit einem angegebenen Anzeigenamen.

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();