我有一个需要在收件箱中的电子邮件主题中查找给定单词的应用程序。我的代码如下:
outlook = new OL.Application();
outlookNameSpace = outlook.GetNamespace("mapi");
outlookNameSpace.Logon(Type.Missing, Type.Missing, false, true);
inbox = outlookNameSpace.GetDefaultFolder(OL.OlDefaultFolders.olFolderInbox);
inboxItems = inbox.Items;
string filter = "@SQL =\"http://schemas.microsoft.com/mapi/proptag/0x0037001f\" LIKE 'Michigan'";
OL.Search advancedSearch = outlook.AdvancedSearch("'inbox'", filter);
当我执行最后一条语句(advancedSearch = ....)时,出现以下错误消息。
发生类型为'System.Runtime.InteropServices.COMException'的未处理异常
其他信息:操作失败。
一般来说,我是高级搜索和DASL查询的新手,所以我的错误可能非常基本。
我可以遍历inboxItems来找到相关的项目,但是在大型收件箱(我的情况下为6700个项目)上,这相当慢
答案 0 :(得分:0)
尝试使用PR_NORMALIZED_SUBJECT
属性(DASL名称为http://schemas.microsoft.com/mapi/proptag/0x0E1D001F
)代替PR_SUBJECT
。
此外,您没有为LIKE
运算符指定任何通配符。
答案 1 :(得分:0)
如果您使用Outlook对象模型中的内置属性,则可以使用以下名称空间:
urn:schemas:httpmail:subject
因此,过滤器可能如下所示:
string filter = "urn:schemas:mailheader:subject LIKE \'%"+ wordInSubject +"%\'";
下面提供了示例代码:
using Outlook = Microsoft.Office.Interop.Outlook;
// ...
string advancedSearchTag = "Our first advanced search in Outlook";
private void RunAdvancedSearch(Outlook._Application OutlookApp, string wordInSubject)
{
string scope = "Inbox";
string filter = "urn:schemas:mailheader:subject LIKE \'%"+ wordInSubject +"%\'";
Outlook.Search advancedSearch = null;
Outlook.MAPIFolder folderInbox = null;
Outlook.MAPIFolder folderSentMail = null;
Outlook.NameSpace ns = null;
try
{
ns = OutlookApp.GetNamespace("MAPI");
folderInbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
folderSentMail = ns.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderSentMail);
scope = "\'" + folderInbox.FolderPath +
"\',\'" + folderSentMail.FolderPath + "\'";
advancedSearch = OutlookApp.AdvancedSearch(
scope, filter, true, advancedSearchTag );
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "An eexception is thrown");
}
finally
{
if(advancedSearch!=null) Marshal.ReleaseComObject(advancedSearch);
if (folderSentMail != null) Marshal.ReleaseComObject(folderSentMail);
if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
if (ns != null) Marshal.ReleaseComObject(ns);
}
}
private void adxOutlookEvents_AdvancedSearchComplete(object sender, object hostObj)
{
Outlook.Search advancedSearch = null;
Outlook.Results advancedSearchResults = null;
Outlook.MailItem resultItem = null;
System.Text.StringBuilder strBuilder = null;
try
{
advancedSearch = hostObj as Outlook.Search;
if (advancedSearch.Tag == advancedSearchTag)
{
advancedSearchResults = advancedSearch.Results;
if (advancedSearchResults.Count > 0)
{
if (HostMajorVersion > 10)
{
object folder = advancedSearch.GetType().InvokeMember("Save",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.InvokeMethod |
System.Reflection.BindingFlags.Public,
null, advancedSearch,
new object[] { advancedSearchTag });
}
else
{
strBuilder = new System.Text.StringBuilder();
strBuilder.AppendLine("Number of items found: " +
advancedSearchResults.Count.ToString());
for (int i = 1; i < = advancedSearchResults.Count; i++)
{
resultItem = advancedSearchResults[i]
as Outlook.MailItem;
if (resultItem != null)
{
strBuilder.Append("#" + i.ToString());
strBuilder.Append(" Subject: " + resultItem.Subject);
strBuilder.Append(" \t To: " + resultItem.To);
strBuilder.AppendLine(" \t Importance: " +
resultItem.Importance.ToString());
Marshal.ReleaseComObject(resultItem);
}
}
if (strBuilder.Length > 0)
System.Diagnostics.Debug.WriteLine(strBuilder.ToString());
else
System.Diagnostics.Debug.WriteLine(
"There are no Mail items found.");
}
}
else
{
System.Diagnostics.Debug.WriteLine("There are no items found.");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "An exception is occured");
}
finally
{
if (resultItem != null) Marshal.ReleaseComObject(resultItem);
if (advancedSearchResults != null)
Marshal.ReleaseComObject(advancedSearchResults);
}
}
您可以在AdvancedSearch文章中了解有关Application类的Advanced search in Outlook programmatically: C#, VB.NET方法的更多信息。