|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
BackgroundIn my last article, I discussed how to read different folders like inbox, sent items etc., and also do a search on the basis of some criteria. My previous articles on this series include:
In this part, first, I'll discuss reading the details of a mail from the inbox, and also cover the mystery behind extended properties. The key points are:
IntroductionEvery mail is of type Reading MailsTo read the details of a mail, we need to use the 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. Setting Read Mails as ReadThere is a property of type bool for public bool SetReadStatus(ItemIdType item)
{
SetItemFieldType setField = new SetItemFieldType();
PathToUnindexedFieldType path = new PathToUnindexedFieldType();
MessageType message = new MessageType();
message.IsRead = true;
message.IsReadSpecified = true;
setField.Item1 = message;
path.FieldURI = UnindexedFieldURIType.messageIsRead;
setField.Item = path;
ItemChangeType[] updatedItems = new ItemChangeType[1];
updatedItems[0] = new ItemChangeType();
updatedItems[0].Updates = new ItemChangeDescriptionType[1];
updatedItems[0].Updates[0] = setField;
ItemChangeDescriptionType[] updates = new ItemChangeDescriptionType[1];
updates[0] = new ItemChangeDescriptionType();
updates[0].Item = path;
updatedItems[0].Item = new ItemIdType();
((ItemIdType)updatedItems[0].Item).Id = item.Id;
((ItemIdType)updatedItems[0].Item).ChangeKey = item.ChangeKey;
UpdateItemType request = new UpdateItemType();
request.ItemChanges = updatedItems;
request.ConflictResolution = ConflictResolutionType.AutoResolve;
request.MessageDisposition = MessageDispositionType.SaveOnly;
request.MessageDispositionSpecified = true;
UpdateItemResponseType response = _esb.UpdateItem(request);
if (response.ResponseMessages.Items[0].ResponseClass !=
ResponseClassType.Success)
return false;
else
return true;
}
Extended Properties - IntroExtended properties are very important for us when we make custom mail applications because, let's say, in a mail, we have default properties in the Extended Properties - How to set themWe can create and set a new property as an extended property at the time of making an instance of the class (i.e., when setting the properties of that class). MessageType p_objMessage=new p_objMessage();
p_objMessage.ExtendedProperty = new ExtendedPropertyType[1];
PathToExtendedFieldType pathClassification = new PathToExtendedFieldType();
pathClassification.DistinguishedPropertySetId =
DistinguishedPropertySetType.PublicStrings;
pathClassification.DistinguishedPropertySetIdSpecified = true;
pathClassification.PropertyName = "Classification";
pathClassification.PropertyType = MapiPropertyTypeType.String;
p_objMessage.ExtendedProperty[0] = new ExtendedPropertyType();
p_objMessage.ExtendedProperty[0].ExtendedFieldURI = pathClassification;
p_objMessage.ExtendedProperty[0].Item = msgRow.Classification;
Here, I am creating an extended property " Extended Properties - How to read themWe have two scenarios here:
For the first case: We first need to specify which property we want to read: PathToExtendedFieldType pathClassification = new PathToExtendedFieldType();
pathClassification.DistinguishedPropertySetId =
DistinguishedPropertySetType.PublicStrings;
pathClassification.DistinguishedPropertySetIdSpecified = true;
pathClassification.PropertyName = "Clasification";
pathClassification.PropertyType = MapiPropertyTypeType.String;
But for using this, we first need to create an object of type ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
itemProperties.BaseShape = DefaultShapeNamesType.Default;
itemProperties.AdditionalProperties = new BasePathToElementType[]
{ pathClassification };
And finally, we need to set this to the findRequest.ItemShape = itemProperties;
For the second case: we first need to read the details by using public ExtendedPropertyType[] GetExtendedProperties(ItemIdType itemid)
{
PathToExtendedFieldType pathClassification = new PathToExtendedFieldType();
pathClassification.DistinguishedPropertySetId =
DistinguishedPropertySetType.PublicStrings;
pathClassification.DistinguishedPropertySetIdSpecified = true;
pathClassification.PropertyName = "Classification";
pathClassification.PropertyType = MapiPropertyTypeType.String;
GetItemType getExPropertiesRequest = new GetItemType();
ItemIdType iiItemId = new ItemIdType();
iiItemId = itemid;
ItemResponseShapeType getResponseShape = new ItemResponseShapeType();
getResponseShape.BaseShape = DefaultShapeNamesType.AllProperties;
getResponseShape.IncludeMimeContent = true;
getExPropertiesRequest.ItemShape = getResponseShape;
getExPropertiesRequest.ItemShape.AdditionalProperties =
new BasePathToElementType[1];
getExPropertiesRequest.ItemShape.AdditionalProperties[0] = pathClassification;
getExPropertiesRequest.ItemIds = new ItemIdType[1];
getExPropertiesRequest.ItemIds[0] = iiItemId;
getExPropertiesRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
GetItemResponseType giResponse = _esb.GetItem(getExPropertiesRequest);
if (giResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
{
throw new Exception("Error: " +
giResponse.ResponseMessages.Items[0].MessageText);
}
else
{
ItemInfoResponseMessageType rmResponseMessage =
giResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
if (rmResponseMessage.Items.Items[0].ExtendedProperty != null)
{
MessageType message = rmResponseMessage.Items.Items[0] as MessageType;
return (message.ExtendedProperty);
}
else
{
return null;
}
}
}
Note: In this article, I hope this series is useful for all those who are working on EWS. Suggestions are highly appreciated. In the next article, I'll start exploring the Calendaring provided by EWS.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||