Im trying to write a simple sample program which checks for any new mail on an Exchange 2010 server. As far as I can tell, the code I've got should work fine.
I setup the service as follows:
ExchangeService service = new ExchangeService();
service.Credentials = new WebCredentials("user@domain.co.uk", "password");
service.Url = new Uri("https://address/owa");
Upon executing the following code:
int unreadMail = 0;
// Add a search filter that searches on the body or subject.
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Defense"));
// Create the search filter.
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
// Create a view with a page size of 50.
ItemView view = new ItemView(50);
// Identify the Subject and DateTimeReceived properties to return.
// Indicate that the base property will be the item identifier
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived);
// Order the search results by the DateTimeReceived in descending order.
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
// Set the traversal to shallow. (Shallow is the default option; other options are Associated and SoftDeleted.)
view.Traversal = ItemTraversal.Shallow;
// Send the request to search the Inbox and get the results.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
// Process each item.
foreach (Item myItem in findResults.Items)
{
if (myItem is EmailMessage)
{
if (myItem.IsNew) unreadMail++;
}
}
I get this error (on the FindItemResults line):
'>' is an unexpected token. The expected token is '"' or '''. Line 1, position 63.
This appears to be an error in the XML the API is actually generating, I've tried a few different lots of code (all along the same lines) and not found anything that works.
Any ideas? At a bit of a loss when it comes directly from the API!
Cheers, Daniel.
Nevermind, fixed it - needed to point my service to:
https://servername/ews/Exchange.asmx
and provide regular domain login details such as "username", "password" in order to connect!