.Net, Data Sources, MS Exchange, MS Office, MS Outlook, Outlook, Outlook 2010, Programming, Software, VB.NET, Visual Studio, VSTO

Programatically Connecting to MS Exchange Using EWS

You can do a lot with your code when you write an application that communicates with your Exchange server. But getting the setup to work for you in Visual Studio isn’t always as intuitive as it should be. Here are the steps I went through getting my project in Visual Studio set up and connecting to MS Exchange.

Step 1: Adding the MS EWS libraries to your Visual Studio Project

I found there are a few gotchas to look out for if you are writing an application that connects to and queries an Exchange server.

EWS is open source framework by Microsoft that lets you connect to and query an Exchange server (cloud or a local server). The simplest way to get the necessary files for your project is using NuGet in Visual Studio. Here’s how:

  1. Open your project in Visual Studio
  2. Right-click the Project menu option in the Solution Explorer window
  3. Select Manage NuGet Packages
  4. Make sure that in the top tabs, you are in the Browse tab (rather than Installed or Updates)
  5. Type ‘EWS’ into the search area
  6. Look for the option: Microsoft.Exchange.Webservices (Exchange Web Services (EWS) Managed API)
  7. When you click on the option it should be the latest version (right now: v2.2.0)
  8. Click to Install

You should now see two new references to the EWS libraries:

Microsoft.Exchanges.WebServices
Microsoft.Exchanges.WebServices.Auth

Great, now that your project has those two references added, you can write a connection to your Exchange server. In this example, I’m connecting using a Windows Classic console app project written in VB.Net (a command prompt application).

Step 2) Writing a Simple Connection to your Exchange Server

Once you have the Microsoft Exchange Webservices added to your project, you are ready to reference the libraries in your code and to write a simple query. In the example code below, I’ve set up a command prompt application in Visual Studio 2017. The code is in VB.NET, but doing this in C# is the virtually the same thing, obviously with some slight syntax differences.

These are the libraries I needed to import to get the example code to work:

Imports Microsoft.Exchange.WebServices

Imports Microsoft.Exchange.WebServices.Auth
Imports Microsoft.Exchange.WebServices.Data

Here is the full example code to connect to a local Exchange server as yourself (the person initiating the program) and then loop through your deleted items. Make sure to replace the part of the URI that says ‘serverName.com’ with your actual server name. Also, you should probably put a breakpoint in your code so you can see things happening without looping through the full list of your deleted emails.

Imports Microsoft.Exchange.WebServices
Imports Microsoft.Exchange.WebServices.Auth
Imports Microsoft.Exchange.WebServices.Data

Module Module1

    Sub Main()
        TestExchangeServerConnection()
    End Sub

    Private Sub TestExchangeServerConnection()
        Dim serverURI As New Uri("https://serverName.com/ews/exchange.asmx")
        Dim exch As New Microsoft.Exchange.WebServices.Data.ExchangeService()
        exch.Url = serverURI
        exch.UseDefaultCredentials = False
        exch.Credentials = New System.Net.NetworkCredential("YourUserName", "YourPassword", "YourDomain")
' or to impersonate a network account use: 
' Dim netCredential As Net.NetworkCredential = Net.CredentialCache.DefaultNetworkCredentials
' exch.Credentials = netCredential
        Dim iv As ItemView = New ItemView(999)
        iv.Traversal = ItemTraversal.Shallow
        Dim deletedItems As FindItemsResults(Of Item) = Nothing
        deletedItems = exch.FindItems(WellKnownFolderName.DeletedItems, iv)
        For Each i As Item In deletedItems
            Console.WriteLine(i.Subject)
        Next
    End Sub

End Module

Helpful Hint:

If you don’t know the URL to your Exchange server, find it by:

  1. Locate the Outlook icon in your system tray (in the bottom right)
  2. Click and hold the Control button on your keyboard
  3. Right-click the Outlook icon with your mouse
  4. Click the option: Test Email Auto-Configuration
  5. Your email address should be pre-filled in the Test popup & the checkbox to Use AutoDiscover should be pre-Checked.
  6. Click the Test button
  7. Find the Availability Service URL in the results. This is the URL to your Exchange server to enter into your application.
Advertisement

1 thought on “Programatically Connecting to MS Exchange Using EWS”

  1. Hi, in my work I used Exchange Web Services .NET Core API for Microsoft Exchange server.
    The API offers complete Exchange Web Services functionality including the ability to create/update/move/copy items and folders, search items and folders, send messages, send meetings requests and more.
    There are a number of use cases on the site and the API itself is well documented.
    Link is http://www.independentsoft.de/exchangewebservices/index.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s