不要使用 ItemId 来识别消息,而是使用 EntryID。使用 EntryID,您可以绑定到同一封电子邮件,而无需 ChangeKey。
以下是此类属性的定义:
ExtendedPropertyDefinition EntryIDProperty = new ExtendedPropertyDefinition(0x0FFF, MapiPropertyType.Binary);
当您搜索消息时,请确保您指示 EWS 在检索到的项目列表中包含此类属性。
以下是调用 FindItems 时获取 EntryID 的示例:
ExtendedPropertyDefinition EntryIDProperty = new ExtendedPropertyDefinition(0x0FFF, MapiPropertyType.Binary);
ItemView item_view = new ItemView(10) { PropertySet = new PropertySet(ItemSchema.Id, EntryIDProperty) };
var result = service.FindItems(WellKnownFolderName.Inbox, item_view);
foreach (var item in result.Items)
{
byte[] entry_id = (byte[])item.ExtendedProperties.Single(x => x.PropertyDefinition == EntryIDProperty).Value;
string entry_id_hex = ByteArrayToHexString(entry_id); //This is the entry ID that you should store
}
如果要使用 EmailMessage.Bind,请使用以下方法将 EntryID 转换为 ItemID:
此方法接受字符串 EntryID。
mailbox_address是邮箱的SMTP地址(例如test@domain.com)
'service' 是 ExchangeService 对象。
private ItemId ConvertEntryIdToItemId(string entryid, string mailbox_address, ExchangeService service)
{
AlternateId id = new AlternateId(IdFormat.HexEntryId, entryid, mailbox_address);
AlternateId new_id = (AlternateId)service.ConvertId(id, IdFormat.EwsId);
ItemId item_id = new_id.UniqueId;
return item_id;
}
现在您可以使用返回的 ItemId 来绑定您的 EmailMessages。