Trabajando con Contactos en Exchange Server

Obteniendo Contactos con EWS

Aspose.Email proporciona la EWSClient clase para conectarse a Microsoft Exchange Server utilizando Exchange Web Services. Los fragmentos de código que siguen utilizan Exchange Web Services para leer todos los contactos. El siguiente fragmento de código muestra cómo obtener Contactos con EWS.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of IEWSClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// List all the contacts
System::ArrayPtr<System::SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
for (System::SharedPtr<Contact> contact : contacts)
{
System::SharedPtr<MapiContact> mapiContact = Contact::to_MapiContact(contact);
// Display name and email address
System::Console::WriteLine(System::String(u"Name: ") + mapiContact->get_NameInfo()->get_DisplayName() + u", Email Address: " + mapiContact->get_ElectronicAddresses()->get_Email1());
}

Resolver Contactos utilizando el Nombre del Contacto

El siguiente fragmento de código muestra cómo obtener contactos utilizando el nombre del contacto.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of IEWSClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// List all the contacts
System::ArrayPtr<System::SharedPtr<Contact>> contacts = client->ResolveContacts(u"Changed Name", Aspose::Email::Clients::Exchange::WebService::ExchangeListContactsOptions::FetchPhoto);
for (System::SharedPtr<Contact> contact : contacts)
{
System::SharedPtr<MapiContact> mapiContact = Contact::to_MapiContact(contact);
// Display name and email address
System::Console::WriteLine(System::String(u"Name: ") + mapiContact->get_NameInfo()->get_DisplayName() + u", Email Address: " + mapiContact->get_ElectronicAddresses()->get_Email1());
}

Determinar el Formato de Notas del Contacto

El Contact->get_NotesFormat especifica el tipo de formato de texto de las notas de los contactos definido por el enumerador TextFormat.

Obtener Contacto utilizando el Id

Un contacto específico se puede recuperar del servidor utilizando su id de contacto como se muestra en el siguiente ejemplo de código.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<Contact> fetchedContact = client->GetContact(id);

Agregar Contactos

El método CreateContact() de la clase IEWSClient se puede utilizar para agregar información de contacto a un Exchange Server. El método CreateContact() toma un objeto Contact como parámetro de entrada.

Para agregar contactos a un Exchange Server:

  1. Inicializa el IEWSClient con la dirección y las credenciales.
  2. Inicializa el Contact con las propiedades deseadas.
  3. Llama al método CreateContact() y pasa el contacto a agregar al Exchange Server.

El siguiente fragmento de código demuestra cómo agregar contactos al Exchange Server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
//Create New Contact
System::SharedPtr<Contact> contact = System::MakeObject<Contact>();
//Set general info
contact->set_Gender(Aspose::Email::PersonalInfo::Gender::Male);
contact->set_DisplayName(u"Frank Lin");
contact->set_CompanyName(u"ABC Co.");
contact->set_JobTitle(u"Executive Manager");
//Add Phone numbers
contact->get_PhoneNumbers()->Add([&]{ auto tmp_0 = System::MakeObject<PhoneNumber>(); tmp_0->set_Number(u"123456789"); tmp_0->set_Category(PhoneNumberCategory::get_Home()); return tmp_0; }());
//contact's associated persons
contact->get_AssociatedPersons()->Add([&]{ auto tmp_1 = System::MakeObject<AssociatedPerson>(); tmp_1->set_Name(u"Catherine"); tmp_1->set_Category(AssociatedPersonCategory::get_Spouse()); return tmp_1; }());
contact->get_AssociatedPersons()->Add([&]{ auto tmp_2 = System::MakeObject<AssociatedPerson>(); tmp_2->set_Name(u"Bob"); tmp_2->set_Category(AssociatedPersonCategory::get_Child()); return tmp_2; }());
contact->get_AssociatedPersons()->Add([&]{ auto tmp_3 = System::MakeObject<AssociatedPerson>(); tmp_3->set_Name(u"Merry"); tmp_3->set_Category(AssociatedPersonCategory::get_Sister()); return tmp_3; }());
//URLs
contact->get_Urls()->Add([&]{ auto tmp_4 = System::MakeObject<Url>(); tmp_4->set_Href(u"www.blog.com"); tmp_4->set_Category(UrlCategory::get_Blog()); return tmp_4; }());
contact->get_Urls()->Add([&]{ auto tmp_5 = System::MakeObject<Url>(); tmp_5->set_Href(u"www.homepage.com"); tmp_5->set_Category(UrlCategory::get_HomePage()); return tmp_5; }());
//Set contact's Email address
contact->get_EmailAddresses()->Add([&]{ auto tmp_6 = System::MakeObject<EmailAddress>(); tmp_6->set_Address(u"Frank.Lin@Abc.com"); tmp_6->set_DisplayName(u"Frank Lin"); tmp_6->set_Category(EmailAddressCategory::get_Email1()); return tmp_6; }());
try
{
client->CreateContact(contact);
}
catch (System::Exception& ex)
{
System::Console::WriteLine(ex.get_Message());
}

Actualizar Contactos

La información del contacto se puede actualizar utilizando Microsoft Outlook. Aspose.Email también puede actualizar la información del contacto en el Exchange Server utilizando el Servicio Web de Exchange (EWS). El método IEWSClient->UpdateContact puede actualizar información del contacto en el Exchange Server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
// List all the contacts and Loop through all contacts
System::ArrayPtr<System::SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
System::SharedPtr<Contact> contact = contacts[0];
System::Console::WriteLine(System::String(u"Name: ") + contact->get_DisplayName());
contact->set_DisplayName(u"David Ch");
client->UpdateContact(contact);

Eliminar Contactos

La clase IEWSClient proporciona el DeleteContact para acceder y eliminar contactos de la carpeta de contactos de un Exchange Server. Este método toma el ID del contacto o Contact como parámetro de entrada.

Para eliminar contactos de un Exchange Server:

  1. Inicializa el IEWSClient con la dirección y las credenciales.
  2. Elimina un contacto usando su ID.
  3. Elimina un contacto llamando al método DeleteContact con Contact como parámetro de entrada.

El siguiente fragmento de código te muestra cómo eliminar contactos de un servidor de intercambio utilizando IEWSClient->DeleteContact.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
// Create instance of EWSClient class by giving credentials
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::String strContactToDelete = u"John Teddy";
System::ArrayPtr<System::SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
for (System::SharedPtr<Contact> contact : contacts)
{
if (System::ObjectExt::Equals(contact->get_DisplayName(), strContactToDelete))
{
client->DeleteContact(contact);
}
}
client->Dispose();