EWS - Csharp - List unread inbox messages using a restriction
// C# sample using Exchange Web Service Proxy classes to list unread messages in an inbox using a restriction.
private void FindInFolder()
{
// TODO: Put this code into a winform and call from a button:
// TODO: create 3 text boxes called txtRequest, txtResponse, txtItems
// TODO: Set the properties for the three text boxes to multiline=true, Scrolling=Both and Wrap=false.
// TODO: Change the credentials below to match your server & mailbox
//----------------- Setup basic credentials for test server ----
//------------------
//string sUserName = "administrator"; // TODO: Change
//string sPassword = "MyPassword1"; // TODO: Change
//string sDomain = "mydom"; // TODO: Change
//string sAuthenticate = "Basic"; //"Windows, "Basic" // TODO: Change
//string sEWS_URL = "https://my2k7server.mydom.extest.microsoft.com/EWS/Services.wsdl"; // TODO: Change
//----------------- Setup windows credentials for live server ----
//
string sUserName = ""; // TODO: Change
string sPassword = ""; // TODO: Change
string sDomain = ""; // TODO: Change
string sAuthenticate = "Windows"; //Windows, Basic // TODO: Change
string sEWS_URL = "https://myserver.microsoft.com/ews/exchange.asmx"; // TODO: Change
// ------------------- Lets now bind to Exchange
NetworkCredential nc = null;
if (sAuthenticate == "Windows")
{
nc = System.Net.CredentialCache.DefaultNetworkCredentials;
}
else
{
nc = new NetworkCredential(sUserName, sPassword, sDomain);
}
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Url = sEWS_URL;
esb.Credentials = nc;
//----------------------- main code ---------------
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
// Define which item properties are returned in the response
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape = itemProperties; // Add properties shape to request
// Identify which folders to search to find items
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
// Add folders to request
findItemRequest.ParentFolderIds = folderIDArray;
//Create unread only restriction --------------------------
RestrictionType restriction = new RestrictionType();
IsEqualToType isEqualTo = new IsEqualToType();
PathToUnindexedFieldType pathToFieldType = new PathToUnindexedFieldType();
pathToFieldType.FieldURI = UnindexedFieldURIType.messageIsRead;
FieldURIOrConstantType constantType = new FieldURIOrConstantType();
ConstantValueType constantValueType = new ConstantValueType();
constantValueType.Value = "0";
constantType.Item = constantValueType;
isEqualTo.Item = pathToFieldType;
isEqualTo.FieldURIOrConstant = constantType;
restriction.Item = isEqualTo;
findItemRequest.Restriction = restriction;
// ------------- GetAccessibilityObjectById the stream
using ( StreamWriter myreqWriter = new StreamWriter( "c:\\findItemRequest.xml"))
{
XmlSerializer myReqSerializer = new XmlSerializer(typeof(FindItemType));
myReqSerializer.Serialize(myreqWriter, findItemRequest);
}
txtRequest.Text = File.ReadAllText("c:\\findItemRequest.xml");
// ------------- Send the request and get the response
FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
using (StreamWriter myrespWriter = new StreamWriter("c:\\findItemResponse.xml"))
{
XmlSerializer myRespSerializer = new XmlSerializer(typeof(FindItemResponseType));
myRespSerializer.Serialize(myrespWriter, findItemResponse);
}
txtResponse.Text = File.ReadAllText("c:\\findItemResponse.xml");
// ------------- read returned
FindItemResponseMessageType folder = (FindItemResponseMessageType) findItemResponse.ResponseMessages.Items[0];
ArrayOfRealItemsType folderContents = new ArrayOfRealItemsType();
folderContents = (ArrayOfRealItemsType)folder.RootFolder.Item;
ItemType[] items = folderContents.Items;
string sText = "";
foreach (ItemType curItem in items)
{
sText += "Subject: " +(curItem.Subject.Trim()) + " ";
sText += "DisplayTo: " + (curItem.DisplayTo.Trim()) + " ";
sText += "DateTimeReceived: " + (curItem.DateTimeReceived.TimeOfDay.ToString()) + " ";
sText += "DateTimeReceived: " + (curItem.ItemClass.Trim()) + " ";
sText += "\r\n" ;
Console.WriteLine(curItem.Subject);
}
txtItems.Text = sText;
}