You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# VSTO Outlook按会话ID高级搜索报错,求正确查询语句

问题:Outlook插件高级搜索会话ID时抛出COM异常

System.Runtime.InteropServices.COMException: 'The operation failed.'

在Outlook插件功能区中基于会话ID构造查询执行高级搜索时,每次都会触发上述异常。相关代码如下:

private async void inSearchId_TextChanged(object sender, RibbonControlEventArgs e)
{
var searchText = (sender as RibbonEditBox).Text;
if (searchText.Length < 5) return; // No point searching until theres a decent query to search against

var conversationLinks = await GetConversationsBySearch(searchText);

var filter = BuildFilterFromConversationIds(conversationLinks);

Search search = _app.AdvancedSearch("inbox", filter, true, "CustomQuery");
search.Results.Sort("[ReceivedTime]", OlSortOrder.olDescending);
}

BuildFilterFromConversationIds方法返回的查询语句尝试过两种写法,均触发异常:
1.

@SQL="http://schemas.microsoft.com/mapi/proptag/0x3013001E" = '22E6722D04D84704B28B060C1CA5AB92'
@SQL="urn:schemas:httpmail:conversationid" = '22E6722D04D84704B28B060C1CA5AB92'

但测试如下查询语句可正常运行:

urn:schemas:httpmail:subject LIKE '%Test%'

请问按会话ID搜索的正确查询语句是什么?为何会抛出该异常?


解决方案

问题原因

Outlook的AdvancedSearch方法使用SQL语法查询会话ID时,会话ID要求以二进制格式传入,而非你当前使用的十六进制字符串格式。直接传入十六进制字符串会导致查询解析失败,触发"The operation failed"异常。

正确查询写法

需要将十六进制的会话ID用hex()函数包裹,转换为二进制格式后再作为查询值。正确的SQL查询语句格式如下:

@SQL="http://schemas.microsoft.com/mapi/proptag/0x3013001E" = hex('22E6722D04D84704B28B060C1CA5AB92')

或使用命名空间的写法:

@SQL="urn:schemas:httpmail:conversationid" = hex('22E6722D04D84704B28B060C1CA5AB92')

代码调整建议

在BuildFilterFromConversationIds方法中,生成查询语句时给每个会话ID加上hex()包裹,示例代码片段:

private string BuildFilterFromConversationIds(List<string> conversationIds)
{
    var idClauses = conversationIds.Select(id => $"\"urn:schemas:httpmail:conversationid\" = hex('{id}')");
    return $"@SQL={string.Join(" OR ", idClauses)}";
}

额外说明

  • 从Outlook对象模型获取的ConversationID属性本身就是十六进制字符串,直接用hex()包裹即可用于查询。
  • 确认AdvancedSearch的作用域参数(第一个参数)正确,若需搜索多个文件夹,可用逗号分隔的文件夹路径,或使用"All"搜索所有可搜索文件夹。

内容的提问来源于stack exchange,提问作者Ryan Durrant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 00:26:21