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:
- Open your project in Visual Studio
- Right-click the Project menu option in the Solution Explorer window
- Select Manage NuGet Packages
- Make sure that in the top tabs, you are in the Browse tab (rather than Installed or Updates)
- Type ‘EWS’ into the search area
- Look for the option: Microsoft.Exchange.Webservices (Exchange Web Services (EWS) Managed API)
- When you click on the option it should be the latest version (right now: v2.2.0)
- Click to Install
You should now see two new references to the EWS libraries:
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:
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
If you don’t know the URL to your Exchange server, find it by:
- Locate the Outlook icon in your system tray (in the bottom right)
- Click and hold the Control button on your keyboard
- Right-click the Outlook icon with your mouse
- Click the option: Test Email Auto-Configuration
- Your email address should be pre-filled in the Test popup & the checkbox to Use AutoDiscover should be pre-Checked.
- Click the Test button
- Find the Availability Service URL in the results. This is the URL to your Exchange server to enter into your application.