![Add or Delete Contacts from Microsoft Exchange Server in C#](images/aspose-email-exchange.png#center)
Microsoft Exchange Server is a popular platform that provides various collaboration services like email, calendars, contacts, etc. In the previous post, we have shown you how to read emails from Microsoft Exchange Server. However, you may also need to work with the contacts on the Exchange Server programmatically. In this article, you will learn how to add, delete, or update contacts on Microsoft Exchange Server in C# .NET.
- .NET API to Access Contacts on MS Exchange Server
- Add Contacts to MS Exchange Server
- Delete Contacts from MS Exchange Server
- Update a Contact on MS Exchange Server
C# .NET API to Access Contacts on MS Exchange Server
To work with contacts on Microsoft Exchange Server, we will use Aspose.Email for .NET. It is a well-known API to work with different email clients from within .NET applications. You can either download the API’s DLL or install it from NuGet using the following command.
PM> Install-Package Aspose.Email
Add Contacts to MS Exchange Server in C#
The following are the steps to add contacts to Microsoft Exchange Server in C#.
- First, create and initialize NetworkCredential object with username, password, and domain.
- Then, initialize IEWSClient with mailbox URI and NetworkCredential object.
- Create an object of Contact class and set its properties such as name, job, gender, phone, associated persons, etc.
- Finally, call EWSClient.CreateContact(Contact) to add contact.
The following code sample shows how to add a contact to Microsoft Exchange Server in C#.
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); | |
// Create new contact | |
Contact contact = new Contact(); | |
// Set general info | |
contact.Gender = Gender.Male; | |
contact.DisplayName = "Frank Lin"; | |
contact.CompanyName = "ABC Co."; | |
contact.JobTitle = "Executive Manager"; | |
// Add Phone numbers | |
contact.PhoneNumbers.Add(new PhoneNumber { Number = "123456789", Category = PhoneNumberCategory.Home }); | |
// Contact's associated persons | |
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Catherine", Category = AssociatedPersonCategory.Spouse }); | |
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Bob", Category = AssociatedPersonCategory.Child }); | |
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Merry", Category = AssociatedPersonCategory.Sister }); | |
// URLs | |
contact.Urls.Add(new Url { Href = "www.blog.com", Category = UrlCategory.Blog }); | |
contact.Urls.Add(new Url { Href = "www.homepage.com", Category = UrlCategory.HomePage }); | |
// Set contact's email address | |
contact.EmailAddresses.Add(new EmailAddress { Address = "Frank.Lin@Abc.com", DisplayName = "Frank Lin", Category = EmailAddressCategory.Email1 }); | |
try | |
{ | |
client.CreateContact(contact); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} |
Delete Contacts from MS Exchange Server in C#
You can also delete a contact from the MS Exchange Server. To filter the contacts, you can use the name, email, or any other suitable property. The following are the steps to delete a contact from Microsoft Exchange Server in C#.
- First, initialize IEWSClient object.
- Then, get contacts from MS Exchange Server using IEWSClient.GetContacts(EWSClient.MailboxInfo.ContactsUri) method.
- Loop through the contacts and filter the required one.
- Finally, delete a filtered contact using IEWSClient.DeleteItem(Contact.Id.EWSId, DeletionOptions.DeletePermanently) method.
The following code sample shows how to delete contacts from Microsoft Exchange Server in C#.
// Initialize EWSClient | |
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); | |
string strContactToDelete = "John Teddy"; | |
// Get contacts | |
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri); | |
// Filter contacts | |
foreach (Contact contact in contacts) | |
{ | |
// Delete contact | |
if (contact.DisplayName.Equals(strContactToDelete)) | |
client.DeleteItem(contact.Id.EWSId, DeletionOptions.DeletePermanently); | |
} | |
client.Dispose(); |
Update a Contact on Exchange Server in C#
You can also update a contact on MS Exchange Server using Aspose.Email for .NET. The following are the steps to perform this operation.
- First, create and initialize NetworkCredential object with username, password, and domain.
- Then, initialize IEWSClient with mailbox URI and NetworkCredential object.
- Get contacts from Exchange Server using IEWSClient.GetContacts(EWSClient.MailboxInfo.ContactsUri) method.
- After that, loop through the contacts and filter the desired contact.
- Finally, update the contact’s properties and call IEWSClient.UpdateContact(Contact) to save it.
The following code sample shows how to update a contact on MS Exchange Server in C#.
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); | |
// Initialize EWSClient | |
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials); | |
// List all the contacts and Loop through all contacts | |
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri); | |
// Select desired contact | |
Contact contact = contacts[0]; | |
Console.WriteLine("Name: " + contact.DisplayName); | |
contact.DisplayName = "David Ch"; | |
// Update contact | |
client.UpdateContact(contact); |
Get a Free API License
You can get a free temporary license to use Aspose.Email for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to work with contacts on Microsoft Exchange Server in C#. We have demonstrated how to add, remove, and update contacts from the MS Exchange Server programmatically in C#. Besides, you can explore the documentation to read more about Aspose.Email for .NET. Also, you can ask your questions via our forum.