Working with Contacts on Exchange Server

Getting Contacts with EWS

Aspose.Email provides the EWSClient class to connect to Microsoft Exchange Server using Exchange Web Services. The code snippets that follow use Exchange Web Services to read all the contacts. The following code snippet shows you how to get Contacts with 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());
}

Resolve Contacts using Contact Name

The following code snippet shows you how to get contacts using the contact name.

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

Determining Contact Notes Format

The Contact->get_NotesFormat specifies the contacts' notes text format type defined by TextFormat enumerator.

Fetch Contact using Id

A particular contact can be retrieved from the server using its contact id as shown in the following code sample.

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

Adding Contacts

The CreateContact() method of the IEWSClient class can be used to add Contact information to an Exchange Server. The CreateContact() method takes a Contact object as an input parameter.

To add contacts to an Exchange Server:

  1. Initialize the IEWSClient with address and credentials.
  2. Initialize the Contact object with the desired properties.
  3. Call the CreateContact() method and pass the contact to add to the Exchange Server.

The following code snippet demonstrates adding contacts to the 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());
}

Updating Contacts

Contact information can be updated using Microsoft Outlook. Aspose.Email can also update contact information on Exchange Server using the Exchange Web Service (EWS). The IEWSClient->UpdateContact method can update contact information on 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);

Deleting Contacts

The IEWSClient class provides the DeleteContact to access and delete contacts from an Exchange Server’s contacts folder. This method takes the contact ID or Contact as an input parameter.

To delete contacts from an Exchange Server:

  1. Initialize the IEWSClient with address and credentials.
  2. Delete a contact using its ID.
  3. Delete a contact by calling the DeleteContact method with Contact as an input parameter.

The following code snippet shows you how to delete contacts from an exchange server using 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();