<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6132634073310783141</id><updated>2011-07-08T02:49:11.419+01:00</updated><category term='c#'/><category term='impersonate'/><category term='2010'/><category term='.net'/><category term='managed'/><category term='2007'/><category term='api'/><category term='sdk'/><category term='exchange'/><category term='vb.net'/><category term='mailbox'/><category term='beta'/><title type='text'>The EDM Guy</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://edmguy.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6132634073310783141/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://edmguy.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Andy D</name><uri>http://www.blogger.com/profile/16690153168005943627</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6132634073310783141.post-2871487550238470215</id><published>2010-04-05T22:05:00.005+01:00</published><updated>2010-04-05T22:13:44.744+01:00</updated><title type='text'>Opening the Internal web Browser in Outlook using VSTO</title><content type='html'>It might sound like a simple thing, and when you look at the code it really is, but finding this tiny little snippet has taken absolutley ages to track down.  Anyway, Big big big thanks to Mostafa Elzoghbi and his blog entry which can be found here...&lt;br /&gt;&lt;br /&gt;&lt;a href="http://moustafa-arafa.blogspot.com/2008/06/open-browser-in-outlook-in-vs-add-ins.html"&gt;http://moustafa-arafa.blogspot.com/2008/06/open-browser-in-outlook-in-vs-add-ins.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And, for the impatient,  the code is basically...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;// Use this to find the webbrowser control&lt;br /&gt;Office.CommandBarComboBox webControl1 = (Office.CommandBarComboBox)this.Application.ActiveExplorer().CommandBars.FindControl(26, 1740, missing, missing);&lt;br /&gt;&lt;br /&gt;// use this to navigate to whatever site you want to&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;webControl1.Text = "http://www.iphelion.com";&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Thanks again Mostafa!&lt;br /&gt;&lt;br /&gt;A&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6132634073310783141-2871487550238470215?l=edmguy.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://edmguy.blogspot.com/feeds/2871487550238470215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://edmguy.blogspot.com/2010/04/opening-internal-web-browser-in-outlook.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6132634073310783141/posts/default/2871487550238470215'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6132634073310783141/posts/default/2871487550238470215'/><link rel='alternate' type='text/html' href='http://edmguy.blogspot.com/2010/04/opening-internal-web-browser-in-outlook.html' title='Opening the Internal web Browser in Outlook using VSTO'/><author><name>Andy D</name><uri>http://www.blogger.com/profile/16690153168005943627</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6132634073310783141.post-7553362259121170164</id><published>2010-02-09T17:17:00.003Z</published><updated>2010-02-09T17:51:28.379Z</updated><title type='text'>EWS Managed API through a Proxy Server</title><content type='html'>So I have been doing a bit more EWS stuff recently and part of that was to access an exchange server on the internet from behind a web proxy server.   This threw up an error which had me a bit annoyed and after a fair bit of searching the web for an answer I found the following&lt;br /&gt;&lt;br /&gt;The upshot being:-&lt;br /&gt;&lt;ul&gt;&lt;li&gt;You cannot specify a proxy server/credentials on a service by service basis - BAD TIMES&lt;/li&gt;&lt;li&gt;You CAN tell your application to use the default credentials for any proxy server it encounters while trying to make a request..  The answer lies in the following bit of code&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;C#&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;WebRequest.DefaultWebProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For VB.NET, just remove the semicolon at the end of the line and place it above where you initialise your Exchange connector.  Worked a treat for me ;-)&lt;br /&gt;&lt;br /&gt;All credit to Mr David Claux for finding this one....&lt;br /&gt;&lt;br /&gt;&lt;a href="http://social.technet.microsoft.com/Forums/en/exchangesvrdevelopment/thread/6f3655d9-fdf4-4779-b1f3-afe78621b093"&gt;http://social.technet.microsoft.com/Forums/en/exchangesvrdevelopment/thread/6f3655d9-fdf4-4779-b1f3-afe78621b093&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6132634073310783141-7553362259121170164?l=edmguy.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://edmguy.blogspot.com/feeds/7553362259121170164/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://edmguy.blogspot.com/2010/02/ews-managed-api-through-proxy-server.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6132634073310783141/posts/default/7553362259121170164'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6132634073310783141/posts/default/7553362259121170164'/><link rel='alternate' type='text/html' href='http://edmguy.blogspot.com/2010/02/ews-managed-api-through-proxy-server.html' title='EWS Managed API through a Proxy Server'/><author><name>Andy D</name><uri>http://www.blogger.com/profile/16690153168005943627</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6132634073310783141.post-5889551046467842051</id><published>2009-08-19T17:11:00.030+01:00</published><updated>2009-08-21T08:37:43.406+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='api'/><category scheme='http://www.blogger.com/atom/ns#' term='vb.net'/><category scheme='http://www.blogger.com/atom/ns#' term='managed'/><category scheme='http://www.blogger.com/atom/ns#' term='exchange'/><category scheme='http://www.blogger.com/atom/ns#' term='2010'/><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='sdk'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='beta'/><category scheme='http://www.blogger.com/atom/ns#' term='mailbox'/><category scheme='http://www.blogger.com/atom/ns#' term='impersonate'/><category scheme='http://www.blogger.com/atom/ns#' term='2007'/><title type='text'>The Managed Exchange EWS API (vb.net) - part 1</title><content type='html'>Ok so this is my first blog EVER (and its not even about Electronic Document Management - EDM - how bout that?), so constructive criticim is welcome in the comments but go easy on me - its my first time :-)&lt;br /&gt;&lt;br /&gt;Want to start by saying ... Hurrah!  We have the start of a a managed interface into the Exchange 2007/2010 server environments thanks to the efforts of the Exchange development team (See the &lt;a href="http://msexchangeteam.com/archive/2009/04/21/451126.aspx"&gt;announcement&lt;/a&gt; here - well done guys - keep up the good work)&lt;br /&gt;&lt;br /&gt;So to celebrate this new API, and because I wanted to write my first blog, and I happen to have to write the code anyway,  I decided to share my experiences with you in creating some code for this new interface..&lt;br /&gt;&lt;br /&gt;Now ive only been using this thing a matter of hours so Im learning as I go and dont claim to have ALL of the answers, but wanted to put this out there to help people who are also starting out with it and find some people in similar situations doing different things that we might learn together..&lt;br /&gt;&lt;br /&gt;This post will hopefully serve as a "getting started" guide and also give some example code (something there is a lack of outside the specialist MS forums at the moment).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;Release Candidate launched (the day I posted this blog)...&lt;/span&gt;&lt;br /&gt;Ive just updated this post to be compatible with the RC version of the code as it was literally launched a few mins ago (see announcement &lt;a href="http://social.technet.microsoft.com/Forums/en/exchangesvrdevelopment/thread/7053c628-9227-4cd5-ba9d-ed6fe8d484cb"&gt;here&lt;/a&gt;) ...&lt;br /&gt;&lt;br /&gt;So onwards ...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Audience&lt;br /&gt;&lt;/span&gt;Im going to assume that those reading this are already very familiar with the Visual Studio environment and object orientated programming, using third party APIs and , and have some knowledge of exchange (probably more than me).   &lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The Task&lt;br /&gt;&lt;/span&gt;Fairly simple requirements.  The task is to create a some code that will use an admin account to scan a list of user's mailboxes and permanently delete items in their "Deleted Items" folder that are older than a specified number of days (30).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Note:  &lt;/span&gt;This code does not cover subfolders of the "Deleted Items" folder (I have got this working but dont have time to write it up right now)&lt;br /&gt;&lt;br /&gt;This blog will focus on PURELY the Managed EWS API code to achieve this for a single user,  you can wrap your own services/applications/loading from lists to your hearts content.  I dont want to crowd the Managed EWS API code with standard .NET development stuff as it will get too complex to read.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Approach&lt;/span&gt;&lt;br /&gt;So ive written something similar before to be used on Exchange 2003,  the old project used WebDAV and logged onto peoples mailboxes through this mechanism,  it worked a treat.  However it appears that there are parts of WebDAV that have been removed in Exchange 2007 and in general its on its way to the big API graveyard in the sky (see the "Get your APIs right" section).&lt;br /&gt;&lt;br /&gt;Im using VB.NET for this as it is part of the request from the client.  If you need a hand converting it to C#  either stick it in this magic &lt;a href="http://www.developerfusion.com/tools/convert/csharp-to-vb/"&gt;thing&lt;/a&gt; or leave a comment here and Im sure someone (or I will) help you produce a C# version of the code.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Get your API's right&lt;br /&gt;&lt;/span&gt;First things first,  there are several API's available to interact with exchange from MAPI and CDO to WebDAV and webservices.  There are details of all of these here...&lt;br /&gt;&lt;br /&gt;&lt;a href="http://blogs.msdn.com/exchangedev/archive/2008/05/22/exchange-developer-roadmap.aspx"&gt;List of APIs and their res&lt;/a&gt;&lt;a href="http://blogs.msdn.com/exchangedev/archive/2008/05/22/exchange-developer-roadmap.aspx"&gt;pective fates&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The one we are using here is the BETA of the &lt;span style="font-weight: bold;"&gt;Managed &lt;/span&gt;EWS (Exchange Web Services) API and &lt;span style="font-weight: bold;"&gt;NOT&lt;/span&gt; a set of proxy classes generated from the Exchange web services.&lt;br /&gt;&lt;br /&gt;The EWS generated proxy classes can look similar when searching for documentation on the internet however the Managed EWS API is the one we are using here..  It is true that these managed classes do use the exchange web services but simply adding a reference to an intellisensed library is far easier than generating proxy classes and working with them.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 1:  Getting the DLLs and references sorted out&lt;br /&gt;&lt;/span&gt;First step is to go &lt;a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=e8f38dd1-f123-4a16-b4c8-584d1f84af48"&gt;here&lt;/a&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;and download yourself a copy of the development library.  Install this to somewhere on your machine and then jump into Visual Studio and create yourself a new Windows Application.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Note:&lt;/span&gt; Im using Visual Studio 2005 for this, however 2008 is fine as well.&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;In solution explorer, right click the project and choose "Add Reference" from the menu&lt;/li&gt;&lt;li&gt;In the add reference screen, browse to where you installed the API,  by default this should be "&lt;span style="font-weight: bold;"&gt;C:\Program Files\Microsoft\Exchange\Web Services\1.0&lt;/span&gt;"&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Double click the "&lt;span style="font-weight: bold;"&gt;Microsoft.Exchange.WebServices.dll&lt;/span&gt;" file to add this as a reference.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 2: What you need to get this code working&lt;br /&gt;&lt;/span&gt;You will need the following environment specific information to get this working on your own environment&lt;ul&gt;&lt;li&gt; Login details for an exchange administrator user (this is used to access each of the specified user's mailboxes)&lt;/li&gt;&lt;li&gt;URL to your exchange server web services - takes the format of http://servername/EWS/Exchange.ASMX&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;Step 3: The code&lt;br /&gt;&lt;/span&gt;So now we have a WinForms project with a Form1 and a Reference to the Microsoft.Exchange.WebServices namespace.&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Right click on the project in Solutions manager again, and choose "&lt;span style="font-weight: bold;"&gt;Properties&lt;/span&gt;",  set the "Application Type" dropdown to "Console App" so we can have a windows form but also see the output on screen as well (you can use &lt;span style="font-style: italic;"&gt;Debug.Print()&lt;/span&gt; if you prefer, I use &lt;span style="font-style: italic;"&gt;Console.WriteLine()&lt;/span&gt; as a personal preference.   Close the properties screen when you are done.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Go create a button on the form, you can leave it called "&lt;span style="font-weight: bold;"&gt;Button1&lt;/span&gt;", double click it to get our code block generated and start coding..&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click&lt;br /&gt;&lt;br /&gt;' -- our code will go here ---&lt;br /&gt;&lt;br /&gt;End Sub&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Ok so the first thing to do is create a connection to our exchange web services so first off, stick the imports at the top of your file,&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;Imports Microsoft.Exchange.WebServices&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Imports Microsoft.Exchang&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;e.WebServices.Autodiscover&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Imports Microsoft.Exchange.WebServices.Data&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Next we need (inside our Button1_Click sub) to connect to our exchange server via the web service URL and also specify the version of exchange we are talking to (&lt;span style="font-weight: bold;"&gt;Note: &lt;/span&gt;Only Exchange 2007 SP1 and 2010 are supported).&lt;br /&gt;&lt;br /&gt;So First we define a the connector object...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;Dim exch As ExchangeService = New ExchangeService(ExchangeVersion.Exchange2007_SP1)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Next we need to specify which user to connect to exchange with.  Now in the case of our task, we need to connect as a user who has admin rights over the exchange environment so we can dip in and out of everyones mailboxes.&lt;br /&gt;&lt;br /&gt;For now (we will talk about &lt;span style="font-style: italic;"&gt;AutoDiscover()&lt;/span&gt; some other time), we are going to hard code this URL to our exchange server in the format of &lt;span style="font-weight: bold;"&gt;"https://SERVERNAME/EWS/Exchange.asmx&lt;/span&gt;"&lt;br /&gt;&lt;br /&gt;So throw in the following code...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;exch.Url = "https://SERVERNAME/EWS/Exchange.asmx"&lt;br /&gt;exch.UseDefaultCredentials = False&lt;br /&gt;exch.Credentials = New System.Net.NetworkCredential("ADMIN_USER","ADMIN_PWD", "ADMIN_DOMAIN")&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;Note: &lt;/span&gt;I simply used the normal login credentials of our admin user here rather than FQDN of the user and it seemed to work fine.&lt;br /&gt;&lt;br /&gt;Next we want to be able to impersonate a user so we can go and snoop their email (and other) folders and start to find and rid ourselves of these old emails.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_oqKB8BR9lDU/So0nGVwVXpI/AAAAAAAAAAU/epPuvfJV2Mw/s1600-h/1.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 320px; height: 104px;" src="http://2.bp.blogspot.com/_oqKB8BR9lDU/So0nGVwVXpI/AAAAAAAAAAU/epPuvfJV2Mw/s320/1.jpg" alt="" id="BLOGGER_PHOTO_ID_5371992920395439762" border="0" /&gt;&lt;/a&gt;There are several ways you can resolve to a user account when using the constructor of &lt;span style="font-style: italic;"&gt;ImpersonateUser&lt;/span&gt;&lt;span style="font-style: italic;"&gt;Id&lt;span style="font-weight: bold;"&gt;, &lt;/span&gt;&lt;/span&gt;in this code I am using the &lt;span style="font-weight: bold;"&gt;PrincipalName &lt;/span&gt;which refers to the users full account name on the active directory (see the screenshot below from AD Users And Computers.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;exch.ImpersonateUserId = New ImpersonateUserId(ConnectingIdType.PrincipalName, "edmguy@ad.mydomain.com")&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Ok so now we have set up the conncetor to log on as our admin user and impersonate our user, now we need to connect to a folder to find some items.  So lets take our first folder "DeletedItems" and see if we cant return how many our user has in that folder.&lt;br /&gt;&lt;br /&gt;So to do this we need to do a search on items within a specific folder, and the EWS managed API allows us to do this very easily.&lt;br /&gt;&lt;br /&gt;First we need to set up an ItemView object which determines parameters of our results (this objec holds properties and collections regarding the search results - things like filters, page sizes, fields to return, etc) , too many to explore right now, but something we can come back to in the future, for now lets focus on getting some results out of exchange...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;Dim iv As ItemView = New ItemView(999) ' return first 999 pages&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;The only property we are going to set on our &lt;span style="font-style: italic;"&gt;ItemView&lt;/span&gt; object is the traversal,  there are three options to this setting &lt;span style="font-style: italic;"&gt;Associated, SoftDeleted &lt;/span&gt;and &lt;span style="font-style: italic;"&gt;Shallow.&lt;/span&gt;   We are going to use &lt;span style="font-style: italic;"&gt;Shallow &lt;/span&gt;in this code to return Non-Deleted items in this folder...&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;iv.Traversal = ItemTraversal.Shallow&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Ok so we now have to make a call to the web service to search for items in this folder.  We are given a nice simple generic collection of Item returned form the web service in the form of the &lt;span style="font-style: italic;"&gt;FindItemResults&lt;/span&gt; object.  So to perform a search for items, we need to initialise one of these object like so...&lt;br /&gt;&lt;br /&gt;(&lt;span style="font-weight: bold;"&gt;Note:&lt;/span&gt; Ive split the following line so its easier to read on the blog, you could simply put the call to the service on the same line as the decleration, or bypass the decleration all together and have a for..each that works directly off of the call to the service)&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;Dim deletedItems As FindItemsResults(Of Item) = Nothing&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Next we need to populate this object by making a call to our exchange service using the &lt;span style="font-style: italic;"&gt;FindItems&lt;/span&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;FindItems &lt;/span&gt;has 4 overloaded constructors, two of which allows you to target a particular folder with or without an object to group the results and the other two allow you to target &lt;span style="font-style: italic;"&gt;WellKnownFolders&lt;/span&gt; (such as Inbox, DeletedItems, SentItems, etc) again with or without a folder for you to group results with.&lt;br /&gt;&lt;br /&gt;For the purposes of the blog and getting things working, we are simply going to go for the one that targets a &lt;span style="font-style: italic;"&gt;WellKnownFolder&lt;/span&gt; without any grouping of results&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;deletedItems = exch.FindItems(WellKnownFolderName.DeletedItems, iv)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;So this (when executed) should have populated the &lt;span style="font-style: italic;"&gt;deletedItems&lt;/span&gt; object with a list of &lt;span style="font-style: italic;"&gt;Item&lt;/span&gt; objects which represent emails from our impersonated users "deleted folder" within exchange.&lt;br /&gt;&lt;br /&gt;Just so we can satisfy ourselves that this works,  we can put together a simple for..each loop to go thru the items and write out to the &lt;span style="font-style: italic;"&gt;Console &lt;/span&gt;or &lt;span style="font-style: italic;"&gt;Debug &lt;/span&gt;output screens and run the code to make sure we have the right results...&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;For Each i As Item In deletedItems&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;   Console.WriteLine(i.Subject)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Next&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Assuming everything is ok,  we now need to find out which items in this folder are older than (lets say) 30 days.  The &lt;span style="font-weight: bold;"&gt;quick and dirty &lt;/span&gt;way to do this would be to put a check on the sent or received date inside the loop but there is a better way... By passing &lt;span style="font-style: italic;"&gt;SearchFilters&lt;/span&gt; into our search method.&lt;br /&gt;&lt;br /&gt;So head back to where we declare our&lt;span style="font-style: italic;"&gt;ItemView&lt;/span&gt; object and now we want to add a couple of simple search filters to return any item sent or recieved before 30 days ago.  To do this we need to use the &lt;span style="font-style: italic;"&gt;SearchFilter.SearchFilterCollection&lt;/span&gt; (as we have more than one criteria and we are linking our criteria with the OR operator) and so we first need to delare this like so...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Dim filters As SearchFilter.SearchFilterCollection = New SearchFilter.SearchFilterCollection(LogicalOperator.Or)&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Next we need to say, add a criteria to only return &lt;span style="font-style: italic;"&gt;Items&lt;/span&gt; that have a received date before 30 days ago,  to do this we create a new &lt;span style="font-style: italic;"&gt;SearchFilter&lt;/span&gt; object and add it to our &lt;span style="font-style: italic;"&gt;Filters&lt;/span&gt; collection.  The EWS Managed API provides an enumerator for the &lt;span style="font-style: italic;"&gt;ItemSchema&lt;/span&gt; to make it easy to access the properties of an item.   Our criteria would be defined and added to our &lt;span style="font-style: italic;"&gt;Filters&lt;/span&gt; collection like so...&lt;br /&gt;&lt;span style=";font-family:courier new;font-size:85%;"  &gt;&lt;br /&gt;filters.Add(New SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, DateTime.Today.AddDays(-30)))&lt;br /&gt;&lt;br /&gt;filters.Add(New SearchFilter.IsLessThan(ItemSchema.DateTimeSent, DateTime.Today.AddDays(-30)))&lt;br /&gt;&lt;br /&gt;So now the code block for defining the item view should look something like this...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Dim iv As ItemView = New ItemView(999)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;iv.Traversal = ItemTraversal.Shallow&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Dim filters As SearchFilter.SearchFilterCollection = New SearchFilter.SearchFilterCollection(LogicalOperator.Or)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;filters.Add(New SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, DateTime.Today.AddDays(-30)))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;filters.Add(New SearchFilter.IsLessThan(ItemSchema.DateTimeSent, DateTime.Today.AddDays(-30)))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Now we need to change the actual search line to pass in our filter, like so...&lt;br /&gt;&lt;span style=";font-family:courier new;font-size:85%;"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;Dim deletedItems As FindItemsResults(Of Item) = _exch.FindItems(WellKnownFolderName.DeletedItems, filters, iv)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And that *should* be it, we now have something that connects to exchange,  attached to a users mailbox (based on a string that you can change) and performs a search on their mailbox for items sent or received over 30 days ago.  Now to delete them,  under the Console.WriteLine() statement simply add the code to delete the item...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;i.Delete(DeleteMode.HardDelete)&lt;/span&gt;  ' -- warning! this gets rid of the item permanently&lt;br /&gt;&lt;br /&gt;Add a msgbox at the end so you know when its finished and that brings us nicely to the end of my first blog.  Hope its helpful to someone out there.  There is quite a bit more to explore on this API and I have another upcoming challenge so that might be part 2.&lt;br /&gt;&lt;br /&gt;Please feel free to comment, question, leave improvements to code, etc here.  Like I said at the top, I dont have all the answers but between us Im sure we can work them out.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Full Code Section - for those who dont like to read too much :-)&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Ive left the delete part of the code remarked out as this is dangerous and you may want to test things out before running it on your exchange environment.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;Imports Microsoft.Exchange.WebServices&lt;br /&gt;Imports Microsoft.Exchange.WebServices.Autodiscover&lt;br /&gt;Imports Microsoft.Exchange.WebServices.Data&lt;br /&gt;&lt;br /&gt;Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click&lt;br /&gt;&lt;br /&gt;' -- our code will go here ---&lt;br /&gt;&lt;br /&gt;Dim exch As ExchangeService = New ExchangeService(ExchangeVersion.Exchange2007_SP1)&lt;br /&gt;&lt;br /&gt;exch.Url = "https://SERVERNAME/EWS/Exchange.asmx" '&lt;br /&gt;exch.UseDefaultCredentials = False&lt;br /&gt;exch.Credentials = New System.Net.NetworkCredential("ADMIN_USERNAME","ADMIN_PWD", "ADMIN_DOMAIN") '&lt;br /&gt;exch.ImpersonateUserId = New ImpersonateUserId(ConnectingIdType.PrincipalName, "edmguy@ad.mydomain.com") '&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Dim iv As ItemView = New ItemView(999) '&lt;br /&gt;iv.Traversal = ItemTraversal.Shallow '&lt;br /&gt;&lt;br /&gt;Dim filters As SearchFilter.SearchFilterCollection = New SearchFilter.SearchFilterCollection(LogicalOperator.Or)&lt;br /&gt;&lt;br /&gt;filters.Add(New SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, DateTime.Today.AddDays(-30)))&lt;br /&gt;&lt;br /&gt;filters.Add(New SearchFilter.IsLessThan(ItemSchema.DateTimeSent, DateTime.Today.AddDays(-30)))&lt;br /&gt;&lt;br /&gt;Dim deletedItems As FindItemsResults(Of Item) = Nothing&lt;br /&gt;deletedItems = exch.FindItems(WellKnownFolderName.DeletedItems, filters, iv)&lt;br /&gt;&lt;br /&gt;' -- enum items to be deleted --&lt;br /&gt;For Each i As Item In deletedItems&lt;br /&gt;Console.WriteLine(i.Subject)&lt;br /&gt;' i.Delete(DeleteMode.HardDelete)&lt;br /&gt;Next&lt;br /&gt;&lt;br /&gt;Msgbox("Done!")&lt;br /&gt;&lt;br /&gt;End Sub&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Troubleshooting&lt;/span&gt;&lt;br /&gt;I had quite a bit of fun simply getting the code to log on and trust the server certificate and also got a number of "403 unauthorised" errors when I was coding this for the first time&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Getting Exception : "This property was requested but was not returned by the server."&lt;/span&gt;&lt;br /&gt;I got this error when I ran the code through my deleted items and I had deleted a draft item that had no send or receive date (when using the "how not to do it" code).  Change the &lt;span style="font-style: italic;"&gt;ItemView&lt;/span&gt; object to do the work for you so the array of &lt;span style="font-style: italic;"&gt;Item&lt;/span&gt; objects being returned have all the values you need.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Getting Exception : "could not establish trust relationship for the ssl tls secure channel with authority"?:&lt;/span&gt;&lt;br /&gt;When I was first trying to get this working internally, i kept getting the exception "could not establish trust relationship for the ssl tls secure channel with authority".  This turned out to be a problem with the certificates on our exchange server.   The trusted authority certificate was for the URL "remotemail.company.com" and I was accessing the internal exchange server name which was "exchsvr01".&lt;br /&gt;&lt;br /&gt;To solve this, I placed an entry in my HOSTS file ("c:\windows\system32\drivers\etc\hosts") with the trusted certificate name pointing at the internal IP address and it got round my problems for now until the networks guys can sort out the DNS for me internally.&lt;br /&gt;&lt;br /&gt;Info from wikipedia on hosts files is &lt;a style="font-family: georgia;" href="http://en.wikipedia.org/wiki/Hosts_file"&gt;here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6132634073310783141-5889551046467842051?l=edmguy.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://edmguy.blogspot.com/feeds/5889551046467842051/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://edmguy.blogspot.com/2009/08/managed-exchange-ews-api-vbnet-part-1.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6132634073310783141/posts/default/5889551046467842051'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6132634073310783141/posts/default/5889551046467842051'/><link rel='alternate' type='text/html' href='http://edmguy.blogspot.com/2009/08/managed-exchange-ews-api-vbnet-part-1.html' title='The Managed Exchange EWS API (vb.net) - part 1'/><author><name>Andy D</name><uri>http://www.blogger.com/profile/16690153168005943627</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_oqKB8BR9lDU/So0nGVwVXpI/AAAAAAAAAAU/epPuvfJV2Mw/s72-c/1.jpg' height='72' width='72'/><thr:total>6</thr:total></entry></feed>
