Reading Mails
To read the details of a mail, we need to use the GetItem API provided by EWS, as:
Collapse Copy Code
public void GetSignalDetails(ItemIdType p_strItemId)
{
GetItemType MailId = new GetItemType();
GetItemResponseType mailResponse;
string strMsg = string.Empty;
MailId.ItemIds = new BaseItemIdType[] { idType };
MailId.ItemShape = new ItemResponseShapeType();
MailId.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
mailResponse = _esb.GetItem(MailId);
ArrayOfResponseMessagesType arrMail = p_mailResponse.ResponseMessages;
ResponseMessageType[] responseMessages = arrMail.Items;
foreach (ResponseMessageType respmsg in responseMessages)
{
if (respmsg.ResponseClass == ResponseClassType.Error)
{
throw new Exception("Error: " + respmsg.MessageText);
}
else if (respmsg.ResponseClass == ResponseClassType.Warning)
{
throw new Exception("Error: " + respmsg.MessageText);
}
//check to determine whether the response message is correct type
if (respmsg is ItemInfoResponseMessageType)
{
ItemInfoResponseMessageType createItemResp =
(respmsg as ItemInfoResponseMessageType);
ArrayOfRealItemsType aorit = createItemResp.Items;
foreach (MessageType myitem in aorit.Items)
{
string strSubject = myMessage.Subject;
if (myMessage.From != null)
{
string strFrom = myMessage.From.Item.Name;
}
StringBuilder objTo = new StringBuilder();
if (myMessage.ToRecipients != null)
{
//To be checked only for single recipient
foreach (EmailAddressType email in myMessage.ToRecipients)
{
objTo.Append(email.Name + "[" + email.EmailAddress + "];");
}
}
StringBuilder objCC = new StringBuilder();
if (myMessage.CcRecipients != null)
{
//To be checked only for single recipient
foreach (EmailAddressType email in myMessage.CcRecipients)
{
objCC.Append(email.Name + "[" + email.EmailAddress + "];");
}
}
objSignalDetailsRow.MailBody = myMessage.Body.Value;
}
//Similarly we can read other properties
}
In the same way, we can read mails related to Calendaring, which will be explored in subsequent articles.
public FolderIdType FindFolderID()
{
DistinguishedFolderIdType objSearchRootFolder = new DistinguishedFolderIdType();
objSearchRootFolder.Id = DistinguishedFolderIdNameType.msgfolderroot;
FindFolderType requestFindFolder = new FindFolderType();
requestFindFolder.Traversal = FolderQueryTraversalType.Deep;
requestFindFolder.ParentFolderIds = new DistinguishedFolderIdType[] { objSearchRootFolder };
requestFindFolder.FolderShape = new FolderResponseShapeType();
requestFindFolder.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
//Search filter definition
requestFindFolder.Restriction = new RestrictionType();
#region Contains expression
ContainsExpressionType objContainsExpression = new ContainsExpressionType();
objContainsExpression.ContainmentMode = ContainmentModeType.FullString;
objContainsExpression.ContainmentModeSpecified = true;
objContainsExpression.ContainmentComparison = ContainmentComparisonType.Exact;
objContainsExpression.ContainmentComparisonSpecified = true;
PathToUnindexedFieldType objFieldFolderName = new PathToUnindexedFieldType();
objFieldFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;
objContainsExpression.Item = objFieldFolderName;
objContainsExpression.Constant = new ConstantValueType();
objContainsExpression.Constant.Value = "Inbox";
#endregion Contains expression
requestFindFolder.Restriction.Item = objContainsExpression;
FindFolderResponseType objFindFolderResponse =
_esb.FindFolder(requestFindFolder);
if (objFindFolderResponse.ResponseMessages.Items.Length == 0)
return null;
foreach (ResponseMessageType responseMsg in
objFindFolderResponse.ResponseMessages.Items)
{
if (responseMsg.ResponseClass == ResponseClassType.Success)
{
FindFolderResponseMessageType objFindResponse =
responseMsg as FindFolderResponseMessageType;
foreach (
BaseFolderType objFolderType in objFindResponse.RootFolder.Folders)
{
return objFolderType.FolderId;
}
}
}
return null;
}
Getting the Count of the Items in a Specified Folder
If the folder contains no Item, It'll return -1.
Collapse Copy Code
public int GetFolderItemsCount()
{
FindItemType findRequest = new FindItemType();
findRequest.ItemShape = new ItemResponseShapeType();
findRequest.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;
BaseFolderIdType p_folder = FindFolderID();
findRequest.ParentFolderIds = new BaseFolderIdType[] { p_folder };
findRequest.Traversal = ItemQueryTraversalType.Shallow;
// Perform the inbox search
FindItemResponseType response = _esb.FindItem(findRequest);
FindItemResponseMessageType responseMessage =
response.ResponseMessages.Items[0]
as FindItemResponseMessageType;
if (responseMessage.ResponseCode != ResponseCodeType.NoError)
{
throw new Exception(responseMessage.MessageText);
}
else
{
if (responseMessage != null && responseMessage.RootFolder != null)
{
return responseMessage.RootFolder.TotalItemsInView;
}
else
{
return -1;
}
}
}
Getting the Count of the Unread Items in a Specified Folder
Collapse Copy Code
public int GetUnreadFolderItemsCount()
{
int unReadCount = -1;
// Identify the folder properties to return.
FolderResponseShapeType properties = new FolderResponseShapeType();
PathToUnindexedFieldType ptuft = new PathToUnindexedFieldType();
ptuft.FieldURI = UnindexedFieldURIType.folderManagedFolderInformation;
PathToUnindexedFieldType[] ptufts = new PathToUnindexedFieldType[1] { ptuft };
properties.AdditionalProperties = ptufts;
properties.BaseShape = DefaultShapeNamesType.AllProperties;
// Form the get folder request.
BaseFolderIdType p_folder = FindFolderID();
GetFolderType request = new GetFolderType();
request.FolderIds = new BaseFolderIdType[1] { p_folder };
request.FolderShape = properties;
// Send the request and get the response.
GetFolderResponseType response = _esb.GetFolder(request);
ArrayOfResponseMessagesType aormt = response.ResponseMessages;
ResponseMessageType[] rmta = aormt.Items;
foreach (ResponseMessageType rmt in rmta)
{
if (rmt.ResponseClass == ResponseClassType.Error)
{
throw new Exception(rmt.MessageText);
}
else
{
FolderInfoResponseMessageType firmt;
firmt = (rmt as FolderInfoResponseMessageType);
BaseFolderType[] folders = firmt.Folders;
foreach (BaseFolderType rfolder in folders)
{
if (rfolder is FolderType)
{
FolderType myFolder;
myFolder = (rfolder as FolderType);
if (myFolder.UnreadCountSpecified)
{
unReadCount = myFolder.UnreadCount;
}
}
}
}
}
return unReadCount;
}
Readings the Items in a Specified Folder
Collapse Copy Code
public List<messagetype> GetFolderItems()
{
FindItemType findRequest = new FindItemType();
findRequest.ItemShape = new ItemResponseShapeType();
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
itemProperties.BaseShape = DefaultShapeNamesType.Default;
findRequest.ItemShape = itemProperties;
Set the inbox as the parent search folder in search attachementRequest.
BaseFolderIdType p_folder = FindFolderID();
findRequest.ParentFolderIds = new BaseFolderIdType[] { p_folder };
findRequest.Traversal = ItemQueryTraversalType.Shallow;
// Perform the inbox search
FindItemResponseType response = _esb.FindItem(findRequest);
FindItemResponseMessageType responseMessage =
response.ResponseMessages.Items[0]
as FindItemResponseMessageType;
if (responseMessage.ResponseCode != ResponseCodeType.NoError)
{
throw new Exception(responseMessage.MessageText);
}
else
{
// Set the next index to use
ArrayOfRealItemsType items = responseMessage.RootFolder.Item as
ArrayOfRealItemsType;
if (items.Items == null)
{
// no items in the view.
return null;
}
// Create our response list
List<messagetype> result = new List<messagetype>(items.Items.Length);
foreach (MessageType item in items.Items)
{
result.Add(item);
}
return result;
}
}
For sorting, first we need to select on which field I want to sort, here I selected Subject:
Collapse Copy Code
FieldOrderType[] fieldsOrder = new FieldOrderType[1];
fieldsOrder[0] = new FieldOrderType();
PathToUnindexedFieldType pathSortField = new PathToUnindexedFieldType();
pathSortField.FieldURI = UnindexedFieldURIType.itemSubject;
fieldsOrder[0].Order = SortDirectionType.Descending;
findRequest.SortOrder = fieldsOrder;
For indexed view (i. e. if I want to bring only 10 items at a time from server) we can set set the max item returned and can set the startitem I'd like.
Collapse Copy Code
IndexedPageViewType indexedView = new IndexedPageViewType();
indexedView.BasePoint = IndexBasePointType.Beginning;
indexedView.MaxEntriesReturned = p_intMaxRec;
indexedView.MaxEntriesReturnedSpecified = true;
indexedView.Offset = p_intStartIdx;
findRequest.Item = indexedView;
Here, also if I want some special properties then I can do like below:
Collapse Copy Code
PathToUnindexedFieldType pathSentDateTime = new PathToUnindexedFieldType();
pathSentDateTime.FieldURI = UnindexedFieldURIType.itemDateTimeSent;
itemProperties.AdditionalProperties = new BasePathToElementType[] { pathSentDateTime;}
findRequest.ItemShape = itemProperties;
Searching the Folders
For searching any folder we need to to do the same as above for all those properties. We also need here one other object of class RestrictionType as:
Collapse Copy Code
RestrictionType objRestrictionType = new RestrictionType();
In searching, we have two options:
search on single property like subject or sentdatetime etc.
search on multiple properties like subject and sentdatetime and etc.
For single property searching we need to create the object of OrType as
Collapse Copy Code
OrType orType = new OrType();
Else for multiple
Collapse Copy Code
AndType andType = new AndType();
And another one object of type as ContainsExpressionType
Here ContainsRxpressionType is used because if, let’s say, the subject is hello and we give the value just “hel” then it’ll search all the items in which the subject value contains “hel.”
Collapse Copy Code
ContainsExpressionType expressionType = null;
List<searchexpressiontype> searchExps = null;
Now for searching for subject
Collapse Copy Code
PathToUnindexedFieldType path.FieldURI = UnindexedFieldURIType.itemSubject;
expressionType = new ContainsExpressionType();
expressionType.Item = path;
expressionType.ContainmentMode = ContainmentModeType.Substring; //seach for substrings
expressionType.ContainmentModeSpecified = true;
expressionType.ContainmentComparison =
ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
expressionType.ContainmentComparisonSpecified = true;
expressionType.Constant = new ConstantValueType();
expressionType.Constant.Value = “Hi”; //search for any string containing
searchExps.Add(expressionType);
To read the details of a mail, we need to use the GetItem API provided by EWS, as:
Collapse Copy Code
public void GetSignalDetails(ItemIdType p_strItemId)
{
GetItemType MailId = new GetItemType();
GetItemResponseType mailResponse;
string strMsg = string.Empty;
MailId.ItemIds = new BaseItemIdType[] { idType };
MailId.ItemShape = new ItemResponseShapeType();
MailId.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
mailResponse = _esb.GetItem(MailId);
ArrayOfResponseMessagesType arrMail = p_mailResponse.ResponseMessages;
ResponseMessageType[] responseMessages = arrMail.Items;
foreach (ResponseMessageType respmsg in responseMessages)
{
if (respmsg.ResponseClass == ResponseClassType.Error)
{
throw new Exception("Error: " + respmsg.MessageText);
}
else if (respmsg.ResponseClass == ResponseClassType.Warning)
{
throw new Exception("Error: " + respmsg.MessageText);
}
//check to determine whether the response message is correct type
if (respmsg is ItemInfoResponseMessageType)
{
ItemInfoResponseMessageType createItemResp =
(respmsg as ItemInfoResponseMessageType);
ArrayOfRealItemsType aorit = createItemResp.Items;
foreach (MessageType myitem in aorit.Items)
{
string strSubject = myMessage.Subject;
if (myMessage.From != null)
{
string strFrom = myMessage.From.Item.Name;
}
StringBuilder objTo = new StringBuilder();
if (myMessage.ToRecipients != null)
{
//To be checked only for single recipient
foreach (EmailAddressType email in myMessage.ToRecipients)
{
objTo.Append(email.Name + "[" + email.EmailAddress + "];");
}
}
StringBuilder objCC = new StringBuilder();
if (myMessage.CcRecipients != null)
{
//To be checked only for single recipient
foreach (EmailAddressType email in myMessage.CcRecipients)
{
objCC.Append(email.Name + "[" + email.EmailAddress + "];");
}
}
objSignalDetailsRow.MailBody = myMessage.Body.Value;
}
//Similarly we can read other properties
}
In the same way, we can read mails related to Calendaring, which will be explored in subsequent articles.
public FolderIdType FindFolderID()
{
DistinguishedFolderIdType objSearchRootFolder = new DistinguishedFolderIdType();
objSearchRootFolder.Id = DistinguishedFolderIdNameType.msgfolderroot;
FindFolderType requestFindFolder = new FindFolderType();
requestFindFolder.Traversal = FolderQueryTraversalType.Deep;
requestFindFolder.ParentFolderIds = new DistinguishedFolderIdType[] { objSearchRootFolder };
requestFindFolder.FolderShape = new FolderResponseShapeType();
requestFindFolder.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
//Search filter definition
requestFindFolder.Restriction = new RestrictionType();
#region Contains expression
ContainsExpressionType objContainsExpression = new ContainsExpressionType();
objContainsExpression.ContainmentMode = ContainmentModeType.FullString;
objContainsExpression.ContainmentModeSpecified = true;
objContainsExpression.ContainmentComparison = ContainmentComparisonType.Exact;
objContainsExpression.ContainmentComparisonSpecified = true;
PathToUnindexedFieldType objFieldFolderName = new PathToUnindexedFieldType();
objFieldFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;
objContainsExpression.Item = objFieldFolderName;
objContainsExpression.Constant = new ConstantValueType();
objContainsExpression.Constant.Value = "Inbox";
#endregion Contains expression
requestFindFolder.Restriction.Item = objContainsExpression;
FindFolderResponseType objFindFolderResponse =
_esb.FindFolder(requestFindFolder);
if (objFindFolderResponse.ResponseMessages.Items.Length == 0)
return null;
foreach (ResponseMessageType responseMsg in
objFindFolderResponse.ResponseMessages.Items)
{
if (responseMsg.ResponseClass == ResponseClassType.Success)
{
FindFolderResponseMessageType objFindResponse =
responseMsg as FindFolderResponseMessageType;
foreach (
BaseFolderType objFolderType in objFindResponse.RootFolder.Folders)
{
return objFolderType.FolderId;
}
}
}
return null;
}
Getting the Count of the Items in a Specified Folder
If the folder contains no Item, It'll return -1.
Collapse Copy Code
public int GetFolderItemsCount()
{
FindItemType findRequest = new FindItemType();
findRequest.ItemShape = new ItemResponseShapeType();
findRequest.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;
BaseFolderIdType p_folder = FindFolderID();
findRequest.ParentFolderIds = new BaseFolderIdType[] { p_folder };
findRequest.Traversal = ItemQueryTraversalType.Shallow;
// Perform the inbox search
FindItemResponseType response = _esb.FindItem(findRequest);
FindItemResponseMessageType responseMessage =
response.ResponseMessages.Items[0]
as FindItemResponseMessageType;
if (responseMessage.ResponseCode != ResponseCodeType.NoError)
{
throw new Exception(responseMessage.MessageText);
}
else
{
if (responseMessage != null && responseMessage.RootFolder != null)
{
return responseMessage.RootFolder.TotalItemsInView;
}
else
{
return -1;
}
}
}
Getting the Count of the Unread Items in a Specified Folder
Collapse Copy Code
public int GetUnreadFolderItemsCount()
{
int unReadCount = -1;
// Identify the folder properties to return.
FolderResponseShapeType properties = new FolderResponseShapeType();
PathToUnindexedFieldType ptuft = new PathToUnindexedFieldType();
ptuft.FieldURI = UnindexedFieldURIType.folderManagedFolderInformation;
PathToUnindexedFieldType[] ptufts = new PathToUnindexedFieldType[1] { ptuft };
properties.AdditionalProperties = ptufts;
properties.BaseShape = DefaultShapeNamesType.AllProperties;
// Form the get folder request.
BaseFolderIdType p_folder = FindFolderID();
GetFolderType request = new GetFolderType();
request.FolderIds = new BaseFolderIdType[1] { p_folder };
request.FolderShape = properties;
// Send the request and get the response.
GetFolderResponseType response = _esb.GetFolder(request);
ArrayOfResponseMessagesType aormt = response.ResponseMessages;
ResponseMessageType[] rmta = aormt.Items;
foreach (ResponseMessageType rmt in rmta)
{
if (rmt.ResponseClass == ResponseClassType.Error)
{
throw new Exception(rmt.MessageText);
}
else
{
FolderInfoResponseMessageType firmt;
firmt = (rmt as FolderInfoResponseMessageType);
BaseFolderType[] folders = firmt.Folders;
foreach (BaseFolderType rfolder in folders)
{
if (rfolder is FolderType)
{
FolderType myFolder;
myFolder = (rfolder as FolderType);
if (myFolder.UnreadCountSpecified)
{
unReadCount = myFolder.UnreadCount;
}
}
}
}
}
return unReadCount;
}
Readings the Items in a Specified Folder
Collapse Copy Code
public List<messagetype> GetFolderItems()
{
FindItemType findRequest = new FindItemType();
findRequest.ItemShape = new ItemResponseShapeType();
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
itemProperties.BaseShape = DefaultShapeNamesType.Default;
findRequest.ItemShape = itemProperties;
Set the inbox as the parent search folder in search attachementRequest.
BaseFolderIdType p_folder = FindFolderID();
findRequest.ParentFolderIds = new BaseFolderIdType[] { p_folder };
findRequest.Traversal = ItemQueryTraversalType.Shallow;
// Perform the inbox search
FindItemResponseType response = _esb.FindItem(findRequest);
FindItemResponseMessageType responseMessage =
response.ResponseMessages.Items[0]
as FindItemResponseMessageType;
if (responseMessage.ResponseCode != ResponseCodeType.NoError)
{
throw new Exception(responseMessage.MessageText);
}
else
{
// Set the next index to use
ArrayOfRealItemsType items = responseMessage.RootFolder.Item as
ArrayOfRealItemsType;
if (items.Items == null)
{
// no items in the view.
return null;
}
// Create our response list
List<messagetype> result = new List<messagetype>(items.Items.Length);
foreach (MessageType item in items.Items)
{
result.Add(item);
}
return result;
}
}
For sorting, first we need to select on which field I want to sort, here I selected Subject:
Collapse Copy Code
FieldOrderType[] fieldsOrder = new FieldOrderType[1];
fieldsOrder[0] = new FieldOrderType();
PathToUnindexedFieldType pathSortField = new PathToUnindexedFieldType();
pathSortField.FieldURI = UnindexedFieldURIType.itemSubject;
fieldsOrder[0].Order = SortDirectionType.Descending;
findRequest.SortOrder = fieldsOrder;
For indexed view (i. e. if I want to bring only 10 items at a time from server) we can set set the max item returned and can set the startitem I'd like.
Collapse Copy Code
IndexedPageViewType indexedView = new IndexedPageViewType();
indexedView.BasePoint = IndexBasePointType.Beginning;
indexedView.MaxEntriesReturned = p_intMaxRec;
indexedView.MaxEntriesReturnedSpecified = true;
indexedView.Offset = p_intStartIdx;
findRequest.Item = indexedView;
Here, also if I want some special properties then I can do like below:
Collapse Copy Code
PathToUnindexedFieldType pathSentDateTime = new PathToUnindexedFieldType();
pathSentDateTime.FieldURI = UnindexedFieldURIType.itemDateTimeSent;
itemProperties.AdditionalProperties = new BasePathToElementType[] { pathSentDateTime;}
findRequest.ItemShape = itemProperties;
Searching the Folders
For searching any folder we need to to do the same as above for all those properties. We also need here one other object of class RestrictionType as:
Collapse Copy Code
RestrictionType objRestrictionType = new RestrictionType();
In searching, we have two options:
search on single property like subject or sentdatetime etc.
search on multiple properties like subject and sentdatetime and etc.
For single property searching we need to create the object of OrType as
Collapse Copy Code
OrType orType = new OrType();
Else for multiple
Collapse Copy Code
AndType andType = new AndType();
And another one object of type as ContainsExpressionType
Here ContainsRxpressionType is used because if, let’s say, the subject is hello and we give the value just “hel” then it’ll search all the items in which the subject value contains “hel.”
Collapse Copy Code
ContainsExpressionType expressionType = null;
List<searchexpressiontype> searchExps = null;
Now for searching for subject
Collapse Copy Code
PathToUnindexedFieldType path.FieldURI = UnindexedFieldURIType.itemSubject;
expressionType = new ContainsExpressionType();
expressionType.Item = path;
expressionType.ContainmentMode = ContainmentModeType.Substring; //seach for substrings
expressionType.ContainmentModeSpecified = true;
expressionType.ContainmentComparison =
ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
expressionType.ContainmentComparisonSpecified = true;
expressionType.Constant = new ConstantValueType();
expressionType.Constant.Value = “Hi”; //search for any string containing
searchExps.Add(expressionType);