【问题标题】:How to get the MessageId from all exchange items如何从所有交换项目中获取 MessageId
【发布时间】:2016-06-02 10:35:47
【问题描述】:

您好,我最近开始围绕 EWS 进行开发。我遇到的一个问题是,一个客户要求我将电子邮件导入数据库,他想根据 InternetMessageID 检测重复,这样他就不必导入重复的电子邮件,我的代码就到了这一点。

 private static string GetInternetMessageID(Microsoft.Exchange.WebServices.Data.Item email)
{
EmailMessage emailMsg = email as EmailMessage;
string returnId = string.Empty;
if ((emailMsg != null)) {
    try {
        emailMsg.Load();
        //loads additional info, without calling this ToRecipients (and more) is empty
    } catch (ArgumentException ex) {
        //retry
        email.Load();
    }
    returnId = emailMsg.InternetMessageId;
} else {
    //what to do?
}
return returnId;

}

我可以处理常规电子邮件,但对于特殊的交换对象,例如联系人、日历、帖子等,它不起作用,因为它无法将其转换为 EmailMessage 对象。

而且我知道您可以从这些对象中提取 internetMessageId。因为客户端曾经有另一个软件为他们提取这个ID,也许该属性不称为internetMessageID,我想我可能必须从internetMessageHeader中提取它。但是,当我尝试从 item 对象中获取它时,它只会给我一个错误。如何从这些“特殊”交换项目中获取 Internet messageID?

PS 我知道 item.id.UniqueID 但这不是我想要的,因为如果我将项目从文件夹移动到另一个文件夹作为交换,这个 id 会发生变化

【问题讨论】:

    标签: c# .net exchange-server exchangewebservices


    【解决方案1】:

    只有通过传输服务发送的对象才会有 InternetMessageId,因此联系人和任务之类的东西,因为它们不是消息并且从未通过传输服务路由,所以永远不会有 Internet MessageId。您可能想看看使用一些属性来执行此操作 InternetMessageId 对消息很有用 PidTagSearchKey https://msdn.microsoft.com/en-us/library/office/cc815908.aspx 是可以使用的(如果您很好的话,有各种使用此属性的示例)。

    如果您要在代码中使用它,请不要使用您使用的方法来加载每个项目的属性,这是非常低效的,因为它将为每个对象进行单独的调用。因为这些 Id 小于 256 Kb,所以在使用 FindItems 时只需检索。例如

            ExtendedPropertyDefinition PidTagSearchKey = new ExtendedPropertyDefinition(0x300B, MapiPropertyType.Binary);
            ExtendedPropertyDefinition PidTagInternetMessageId = new ExtendedPropertyDefinition(0x1035, MapiPropertyType.String);
            PropertySet psPropSet = new PropertySet(BasePropertySet.IdOnly);
            psPropSet.Add(PidTagSearchKey);
            psPropSet.Add(PidTagInternetMessageId);
            ItemView ItemVeiwSet = new ItemView(1000);
            ItemVeiwSet.PropertySet = psPropSet;
            FindItemsResults<Item> fiRess = null;
            do
            {
                fiRess = service.FindItems(WellKnownFolderName.Inbox, ItemVeiwSet);
                foreach (Item itItem in fiRess)
                {
                    Object SearchKeyVal = null;
                    if (itItem.TryGetProperty(PidTagSearchKey, out SearchKeyVal))                                   
                    {
                        Console.WriteLine(BitConverter.ToString((Byte[])SearchKeyVal));
                    }
                    Object InternetMessageIdVal = null;
                    if (itItem.TryGetProperty(PidTagInternetMessageId, out InternetMessageIdVal))                                   
                    {
                        Console.WriteLine(InternetMessageIdVal);
                    }
                }
                ItemVeiwSet.Offset += fiRess.Items.Count;
            } while (fiRess.MoreAvailable);
    

    如果您需要更大的属性,例如 Body 使用 LoadPropertiesForItems 方法https://blogs.msdn.microsoft.com/exchangedev/2010/03/16/loading-properties-for-multiple-items-with-one-call-to-exchange-web-services/

    【讨论】:

    • 我认为有一个的唯一原因是客户端之前有一个应用程序正在提取一个与互联网 messageID 具有相同格式的 ID,这就是我认为它有一个的原因。但它可能是一个不同的 id,就像你提到的那样。
    • 是不是因为当我导出邮件变成msg文件时它有messageid?
    • 使用 EWSEditor 或 MFCMapi 等工具自行测试,它会准确告诉您什么可用或不可用
    • 谢谢,我会测试一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2013-05-31
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多