背景
我在去年的一篇文章中,我讨论了如何读取不同的文件夹如收件箱,已发送的邮件等,并也做了一些标准的基础上的搜索。
我以前在这个系列文章包括:{A}
在这一部分,首先,我将讨论从收件箱中阅读邮件的细节,也包括扩展属性背后的奥秘。其要点是:{A3}{A4}{A5}{A6}{A7}{A8}
每一封邮件的类型MessageType或从MessageType继承,所以我们可以从MessageType甚至到日历相关邮件的收件箱中的所有项目。{A9}
要阅读邮件的细节,我们需要使用的GetItem预警系统提供的API,如: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
}
以同样的方式,我们可以阅读到日历相关的邮件,将在以后的文章中探讨。{A10}
是一个物业类型MessageType BOOL称为isRead。第一次邮件到收件箱,我们以粗体显示,因为它是还没有看过,但读取邮件时,我们需要设置的属性isRead,为true,以便下一次我们显示正常。成功读取邮件后,我们需要更新这个属性为true。我们有一个API UpdateItem更新任何现有的项目。做到这一点的代码所示:{C}
扩展属性是很重要的,对我们时,我们让自定义的电子邮件应用程序,因为,比方说,在邮件中,我们有MessageType项目中的默认属性,并要一个新的属性,让我们说,分类我们的自定义,应用程序,那么我们就需要使用扩展属性。{A12}
我们可以创建和设置一个新的属性作为扩展属性类的一个实例时(例如,设置类的属性时)。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;
在这里,我创建一个扩展属性quot; Classificationquot; MessageType对象,这应该是在设置该对象的其他属性,创建对象后的时候做。{A13}
我们这里有两种情况:在\阅读完所有的文件夹中的项目进行搜索的时间读它。读在详细阅读的时间。
对于第一种情况:我们首先需要指定我们要读哪个属性:PathToExtendedFieldType pathClassification = new PathToExtendedFieldType();
pathClassification.DistinguishedPropertySetId =
DistinguishedPropertySetType.PublicStrings;
pathClassification.DistinguishedPropertySetIdSpecified = true;
pathClassification.PropertyName = "Clasification";
pathClassification.PropertyType = MapiPropertyTypeType.String;
,但使用此,我们首先需要创建一个对象类型ItemResponseShapeType,如下所示设置BaseShape属性:ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
itemProperties.BaseShape = DefaultShapeNamesType.Default;
itemProperties.AdditionalProperties = new BasePathToElementType[]
{ pathClassification };
最后,我们需要设置的findRequest对象,如:findRequest.ItemShape = itemProperties;
对于第二种情况:我们首先需要阅读使用GetItem细节;然后,我们需要呼吁所有的扩展属性EWS的API,因为一走,我们不能这样做。代码如下所示: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;
}
}
}
注:在这篇文章中,_esb ExchangeService对象,因为在我的系列前面的文章中。
我希望这个系列是为所有那些就预警系统的工作非常有用。建议表示高度赞赏。在接下来的文章中,我将开始探索早期预警系统提供的日历。