Match EWS Conversation* to Outlook Add-in Conversation*

懵懂的女人 提交于 2019-12-05 12:48:29
Brad Christie

Just found out (I think).

Breakdown

With more Googling and a bit more effort I believe I was able to make them align 1:1 using the following:

ConversationId

This is apparently an assembled value made up of several properties. Luckily I was able to find a method Woodman posted re-implementing the original algorithm used by Outlook here. With some minor modifications (to work with EWS instead of Outlook) I was able to get it to work.

ConversationIndex

This turned out to simply be a matter of using the BitConverter (and removing the hyphens). Easy peasy.

Final Result:

public static class EwsEmailMessageExtensions
{
    private const int c_ulConvIndexIDOffset = 6;
    private const int c_ulConvIndexIDLength = 16;
    private static ExtendedPropertyDefinition PidTagConversationIndexTracking = new ExtendedPropertyDefinition(0x3016, MapiPropertyType.Boolean);

    // HUGE props to Woodman
    // https://stackoverflow.com/a/21625224/298053
    public static string GetOutlookConversationId(this EmailMessage emailMessage)
    {
        Boolean convTracking;
        if (!emailMessage.TryGetProperty(PidTagConversationIndexTracking, out convTracking))
        {
            convTracking = true;
        }

        var convIndex = emailMessage.ConversationIndex;
        byte[] idBytes;
        if (convTracking && convIndex != null && convIndex.Length > 0)
        {
            // get Id from Conversation index
            idBytes = new byte[c_ulConvIndexIDLength];
            Array.Copy(convIndex, c_ulConvIndexIDOffset, idBytes, 0, c_ulConvIndexIDLength);
        }
        else
        {
            // get Id from Conversation topic
            var topic = emailMessage.ConversationTopic;
            if (string.IsNullOrEmpty(topic))
            {
                return string.Empty;
            }

            if (topic.Length >= 265)
            {
                topic = topic.Substring(0, 256);
            }
            topic = topic.ToUpper();

            using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
            {
                idBytes = md5.ComputeHash(Encoding.Unicode.GetBytes(topic));
            }
        }

        return BitConverter.ToString(idBytes).Replace("-", string.Empty);
    }

    public static String GetOutlookConversationIndex(this EmailMessage emailMessage)
    {
        var convIndex = emailMessage.ConversationIndex;
        return BitConverter.ToString(convIndex).Replace("-", String.Empty);
    }
}

Usage:

// Prep
ExchangeService service = new ExchangeService(...);
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
Item item = /* inbox.FindItems(...).First() */

// Implmentation
EmailMessage emailMessage = item as EmailMessage;
if (emailMessage != null)
{
   String conversationId = emailMessage.GetOutlookConversationId();
   String conversationIndex = emailMessage.GetOutlookConversationIndex();
   /* ... */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!