C#Interop.Outlook查找主题中带有特定单词的邮件 [英] C# Interop.Outlook find messages with specific word in subject
问题描述
我有一个需要在收件箱中的电子邮件主题中查找给定单词的应用程序.我的代码如下:
I have an application that needs to look for a given word in the subject of emails in the inbox. My code looks like this:
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 = ....)时,出现以下错误消息.
When I execute the last statement (advancedSearch = ....), I get the following error message.
发生了类型为'System.Runtime.InteropServices.COMException'的未处理的异常
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred
其他信息:操作失败.
一般来说,我是高级搜索和DASL查询的新手,所以我的错误可能非常基本.
I am new to Advanced Search and DASL queries in general, so my error may be very basic.
我可以遍历inboxItems来找到相关的项目,但是在大型收件箱(我的情况下为6700个项目)中,这相当慢
I can loop through inboxItems to find the relevant items, but that is rather slow on an a large inbox (6700 items in my case)
推荐答案
如果您使用Outlook对象模型中的内置属性,则可以使用以下命名空间:
If you use a built-in property from the Outlook object model you can use the following namespace:
urn:schemas:httpmail:subject
因此,过滤器可能如下所示:
So, the filter may look like this:
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 方法的更多信息在Outlook中以编程方式进行高级搜索:C#,VB.NET 文章.
You can read more about the AdvancedSearch method of the Application class in the Advanced search in Outlook programmatically: C#, VB.NET article.
这篇关于C#Interop.Outlook查找主题中带有特定单词的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!