Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. ASP.NET MVC
  4. Microsoft Outlook Get and Add Contacts Asp.Net MVC

Microsoft Outlook Get and Add Contacts Asp.Net MVC

Date- Oct 09,2020

Updated Mar 2026

13220

Free Download
Microsoft Outlook Contacts Outlook

Overview of Microsoft Exchange Service

The Microsoft Exchange Service provides a robust API that allows developers to access and manipulate various Outlook functionalities, including managing contacts, calendars, and tasks. By utilizing the Exchange Web Services (EWS), developers can create applications that interact with Outlook data directly, enabling features such as contact synchronization and calendar management.

This integration is particularly useful in enterprise applications where managing user data efficiently is critical. For instance, a CRM (Customer Relationship Management) system can utilize this service to fetch and update customer contact details directly from Outlook, ensuring that the data remains consistent across platforms.

Prerequisites

Before diving into the code, ensure you have the following prerequisites:

  • A Microsoft Outlook account with Exchange Online or an on-premises Exchange server.
  • Visual Studio installed on your machine.
  • Basic understanding of ASP.NET MVC and C# programming.
  • Access to NuGet Package Manager to install necessary libraries.

Setting Up the Project

To begin, we need to set up our ASP.NET MVC project and install the necessary NuGet packages. Follow these steps:

  1. Create a new ASP.NET MVC project in Visual Studio.
  2. Open the Package Manager Console and run the following command to install the EWS Managed API:
Install-Package Microsoft.Exchange.WebServices

Once the package is installed, we can start coding our contact management functionalities.

Getting Contacts from Outlook

To retrieve contacts from a user's Outlook account, we will utilize the EWS Managed API. Below is a detailed implementation of the method to get contacts:

public void GetContact() {
    string ewsUrl = "https://outlook.office365.com/EWS/Exchange.asmx";
    string userName = "outlookusername";
    string password = "outlookpassword";
    ExchangeService servicex = new ExchangeService();
    servicex.Url = new Uri(ewsUrl);
    servicex.Credentials = new WebCredentials(userName, password);

    ContactsFolder contactsfolder = ContactsFolder.Bind(servicex, WellKnownFolderName.Contacts);
    int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;

    if (numItems == 0) AddContact();

    ItemView view = new ItemView(numItems);
    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);

    FindItemsResults contactItems = servicex.FindItems(WellKnownFolderName.Contacts, view);

    foreach (Item item in contactItems) {
        if (item is Contact) {
            Contact contact = item as Contact;
            Console.WriteLine(contact.DisplayName);
        }
    }
}

In this method, we establish a connection to the EWS service using the user's credentials. We then bind to the Contacts folder and retrieve a specified number of contacts (up to 50 in this case). The retrieved contacts are displayed using their display names.

Adding Contacts to Outlook

After retrieving contacts, the next logical step is to add new contacts to the user's Outlook account. The following code demonstrates how to accomplish this:

public void AddContact() {
    string ewsUrl = "https://outlook.office365.com/EWS/Exchange.asmx";
    string userName = "outlookusername";
    string password = "outlookpassword";
    ExchangeService servicex = new ExchangeService();
    servicex.Url = new Uri(ewsUrl);
    servicex.Credentials = new WebCredentials(userName, password);

    Contact contact = new Contact(servicex);
    contact.GivenName = "Brian";
    contact.MiddleName = "David";
    contact.Surname = "Johnson";
    contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;

    contact.CompanyName = "Contoso";
    contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "425-555-0110";
    contact.PhoneNumbers[PhoneNumberKey.HomePhone] = "425-555-0120";
    contact.PhoneNumbers[PhoneNumberKey.CarPhone] = "425-555-0130";

    contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress("abc_1@123.com");
    contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress("abv_2@123.com");

    contact.ImAddresses[ImAddressKey.ImAddress1] = "brianIM1@contoso.com";
    contact.ImAddresses[ImAddressKey.ImAddress2] = "brianIM2@contoso.com";

    PhysicalAddressEntry paEntry1 = new PhysicalAddressEntry();
    paEntry1.Street = "123 Main Street";
    paEntry1.City = "Chandigarh";
    paEntry1.State = "WA";
    paEntry1.PostalCode = "11111";
    paEntry1.CountryOrRegion = "India";
    contact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry1;

    PhysicalAddressEntry paEntry2 = new PhysicalAddressEntry();
    paEntry2.Street = "456 Corp Avenue";
    paEntry2.City = "Chandigarh";
    paEntry2.State = "WA";
    paEntry2.PostalCode = "11111";
    paEntry2.CountryOrRegion = "India";
    contact.PhysicalAddresses[PhysicalAddressKey.Business] = paEntry2;

    contact.Save();
}

This method constructs a new contact object, populates it with various details such as name, company, phone numbers, email addresses, and physical addresses, and then saves it to the user's Outlook account.

Edge Cases & Gotchas

When working with the Microsoft Exchange Service, there are several edge cases and potential issues to be aware of:

  • Authentication Failures: Ensure that the provided username and password are correct. Consider implementing OAuth for improved security and user experience.
  • Rate Limiting: Be aware of potential rate limits when making API calls. If too many requests are made in a short period, you may receive errors indicating that you have exceeded the allowed limit.
  • Data Validation: Validate the data before attempting to save contacts. This includes checking for null or invalid fields to avoid exceptions during the save operation.

Performance & Best Practices

To ensure optimal performance when interacting with the Microsoft Exchange Service, consider the following best practices:

  • Batch Processing: If you need to add or retrieve multiple contacts, consider using batch processing techniques to minimize the number of API calls.
  • Caching: Implement caching strategies for frequently accessed data to reduce the load on the Exchange server and improve response times.
  • Asynchronous Programming: Use asynchronous programming patterns to avoid blocking the main thread and improve the responsiveness of your application.

Conclusion

In this tutorial, we learned how to integrate Microsoft Outlook contact management into an ASP.NET MVC application using the Microsoft Exchange Service. We covered how to retrieve and add contacts, explored potential edge cases, and discussed best practices for optimal performance. Key takeaways include:

  • Utilizing the EWS Managed API for seamless integration with Outlook.
  • Understanding how to manage authentication securely.
  • Implementing best practices for efficient data handling and performance.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Implement Stripe Payment Gateway In ASP.NET
Sep 10, 2020
Jquery Full Calender Integrated With ASP.NET
Sep 30, 2020
Microsoft Outlook Add Appointment and Get Appointment using Asp.Net MVC
Oct 03, 2020
How to implement JWT Token Authentication and Validate JWT Token in ASP.NET MVC using JWT
Oct 12, 2022
Previous in ASP.NET MVC
Microsoft Outlook Add Appointment and Get Appointment using Asp.N…
Next in ASP.NET MVC
Payumoney Integration With Asp.Net MVC

Comments

Contents

๐ŸŽฏ

Interview Prep

Ace your ASP.NET MVC interview with curated Q&As for all levels.

View ASP.NET MVC Interview Q&As

More in ASP.NET MVC

  • Payumoney Integration With Asp.Net MVC 23112 views
  • MVC Crud Operation with Interfaces and Repository Pattern wi… 21794 views
  • Using Ajax in Asp.Net MVC 21154 views
  • Stopping Browser Reload On saving file in Visual Studio Asp.… 20573 views
  • Exception Handling and Creating Exception Logs in Asp.net MV… 20411 views
View all ASP.NET MVC posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor