[英]How do I store Outlook email search results into an IEnumerable object (C#)
如何将以下所有(Outlook.Items.Find)结果存储/投射到IEnumerable对象中? *展望16.0
Outlook.MailItem emailResults = null;
emailResults = mailItems.Find($"[Categories] = 'Important'");
我正在尝试以下方法,但没有成功。
application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
nameSpace = application.GetNamespace("mapi");
inbox = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
mailItems = inbox.Items;
IEnumerable<Outlook.MailItem> emailResults = null;
emailResults = (IEnumerable<Outlook.MailItem>)mailItems.Find($"[Categories] = 'Important'");
我正在尝试看中而不是遍历每个结果。 感谢您提前提出任何建议。
Use Items.Restrict
返回可枚举的Items
集合。
感谢Dmitry Streblechenko的快速解答! 这是我的解决方案代码。
using System.Collections.Generic;
using System.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace oMailBoard
{
public static class GetMail
{
public static void SearchOutLook()
{
Outlook.Application application = null;
Outlook.NameSpace nameSpace = null;
Outlook.MAPIFolder inbox = null;
Outlook.Items mailItems = null;
IEnumerable<Outlook.MailItem> emails = null;
string filter = "";
filter = $"[Categories] = 'Important'"; //Exact match
filter = $"@SQL=urn:schemas:httpmail:subject like '%database%'"; //Like match
if (Process.GetProcessesByName("Outlook").Count() > 0) //Is Outlook Open?
{
application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
nameSpace = application.GetNamespace("mapi");
inbox = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
mailItems = inbox.Items;
//Store results into a numerable varible.
emails = mailItems.Restrict(filter).Cast<Outlook.MailItem>();
//Now I can iterate though the results.
foreach (Outlook.MailItem e in emails)
{
Debug.WriteLine("\f");
Debug.WriteLine(e.Subject);
}
Debug.WriteLine(emails.Count());
Marshal.ReleaseComObject(inbox);
Marshal.ReleaseComObject(nameSpace);
Marshal.ReleaseComObject(application);
inbox = null;
nameSpace = null;
application = null;
}
else
{
Debug.WriteLine("Outlook Not Open!");
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.