Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • 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
  1. Home
  2. Blogpost

Microsoft Outlook Get and Add Contacts Asp.Net MVC

Date- Oct 09,2020

13200

Free Download
Microsoft Outlook Contacts Outlook

Step-1 First of all add a nugget package for Microsoft Exchange Services which will let you connect to Outlook from within your web application. We will use this to get our Outlook Contacts and then for saving Contact also.

Step-2 Add these namespaces to your controller

using Microsoft.Exchange.WebServices.Data;
using CalendarView = Microsoft.Exchange.WebServices.Data.CalendarView;

Step-3 we will use this to get contacts from our outlook account. You can replace your outlook username and password  and then just run this piece of code.

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.UseDefaultCredentials = true;
            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();
            numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;
            // Instantiate the item view with the number of items to retrieve from the Contacts folder.
            ItemView view = new ItemView(numItems);

            // To keep the request smaller, request only the display name property.
            view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);

            // Retrieve the items in the Contacts folder that have the properties that you selected.
            FindItemsResults<Item> contactItems = servicex.FindItems(WellKnownFolderName.Contacts, view);

            // Display the list of contacts. 
            foreach (Item item in contactItems)
            {
                if (item is Contact)
                {
                    Contact contact = item as Contact;
                    Console.WriteLine(contact.DisplayName);
                }
            }
        }

Step-4 After getting contacts now in this step we will see how to add new contacts. We will use same exchange service to Add contacts in our Microsoft Outlook account.

 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.UseDefaultCredentials = true;
            servicex.Credentials = new WebCredentials(userName, password);
            Contact contact = new Contact(servicex);
            contact.GivenName = "Brian";
            contact.MiddleName = "David";
            contact.Surname = "Johnson";
            contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;

            // Specify the company name.
            contact.CompanyName = "Contoso";

            // Specify the business, home, and car phone numbers.
            contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "425-555-0110";
            contact.PhoneNumbers[PhoneNumberKey.HomePhone] = "425-555-0120";
            contact.PhoneNumbers[PhoneNumberKey.CarPhone] = "425-555-0130";

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

            // Specify two IM addresses.
            contact.ImAddresses[ImAddressKey.ImAddress1] = "brianIM1@contoso.com";
            contact.ImAddresses[ImAddressKey.ImAddress2] = " brianIM2@contoso.com";

            // Specify the home address.
            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;

            // Specify the business address.
            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;

            // Save the contact.
            contact.Save();
        }

Conclusion- So this is how you can integrate outlook contacts with your Asp.Net MVC web application

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

Comments

Contents

More in ASP.NET MVC

  • Payumoney Integration With Asp.Net MVC 23089 views
  • MVC Crud Operation with Interfaces and Repository Pattern wi… 21781 views
  • Using Ajax in Asp.Net MVC 21129 views
  • Stopping Browser Reload On saving file in Visual Studio Asp.… 20550 views
  • Exception Handling and Creating Exception Logs in Asp.net MV… 20385 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
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
By Language
  • Angular
  • C
  • 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