马顿,
看起来好像您已经掌握了使用扩展属性的正确基本概念。很难说问题出在哪里,因为您的代码只有几个片段。这是一个您应该能够从中构建的示例。它创建一个新的邮件消息,设置扩展属性,然后保存消息(到草稿文件夹)。
// Create a definition for the extended property.
ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "Engineer", MapiPropertyType.String);
// Create an e-mail message that you will add the extended property to.
EmailMessage message = new EmailMessage(service);
message.Subject = "Saved with custom ExtendedPropertyDefinition.";
message.Body = "The Engineer custom value is stored within the extended property.";
message.ToRecipients.Add("user@contoso.com");
// Add the extended property to an e-mail message object.
message.SetExtendedProperty(extendedPropertyDefinition, "Save some customer value");
message.Save();
现在要验证消息是使用扩展属性创建的,您可以使用 FindItems。以下示例将在草稿文件夹中搜索具有“工程师”扩展属性的消息。
ItemView view = new ItemView(10);
// Create a definition for the extended property.
ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "Engineer", MapiPropertyType.String);
// Create a search filter the filters email based on the existence of the extended property.
SearchFilter.Exists customPropertyExistsFilter = new SearchFilter.Exists(extendedPropertyDefinition);
// Create a property set that includes the extended property definition.
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, extendedPropertyDefinition);
// Search the drafts folder with the defined view and search filter.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Drafts, customPropertyExistsFilter, view);
// Search the e-mail collection returned in the results for the extended property.
foreach (Item item in findResults.Items)
{
Console.WriteLine(item.Subject);
// Check whether the item has the custom extended property set.
if (item.ExtendedProperties.Count > 0)
{
// Display the extended name and value of the extended property.
foreach (ExtendedProperty extendedProperty in item.ExtendedProperties)
{
Console.WriteLine(" Extended Property Name: " + extendedProperty.PropertyDefinition.Name);
Console.WriteLine(" Extended Property Value: " + extendedProperty.Value);
}
}
else
{
Console.WriteLine(" This email does not have the 'Engineer' extended property set on it");
}
}
为了在 Outlook 中以自定义表单查看和更新这些扩展属性,需要完成一些额外的工作。Outlook 窗体使用附加属性将扩展属性存储为二进制字段。需要修改 PidLidPropertyDefinitionStream 以及扩展属性。不幸的是,EWS 托管 API 不会为您执行此操作,因此您必须自己编写一些代码来读取/更新此属性。我没有任何示例代码可向您展示,但这里有一些关于属性结构的链接,它们将在此过程中为您提供帮助: