[英]C# searching the Inbox for specific subjects emails
我正在嘗試搜索具有特定主題的特定電子郵件。
Outlook.Folder inbox = new Outlook.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items items = inbox.Items;
Outlook.MailItem mailItem = null;
object folderItem;
string subjectName = string.Empty;
string filter = "[Subject] > 's' And [Subject] <'u'";
folderItem = items.Find(filter);
while (folderItem != null)
{
mailItem = folderItem as Outlook.MailItem;
if (mailItem != null)
{
subjectName += "\n" + mailItem.Subject;
}
folderItem = items.FindNext();
}
subjectName = "The follow e-mail messages were found: " + subjectName;
MessageBox.Show(subjectName);
我收到一個錯誤:“嚴重性代碼描述項目文件行抑制 State 錯誤 CS0426 類型名稱 'ActiveExplorer' 在類型'Application'中不存在”
如果您開發一個獨立的應用程序,其中 Outlook 是自動化的,您需要首先創建一個新的Application
實例,或者如果您開發基於 VSTO 的插件而不是以下代碼,則使用內置屬性:
Outlook.Folder inbox = new Outlook.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
一個獨立的應用程序應該創建一個新的 Outlook 實例:
Outlook.Application app = new Outlook.Application();
Outlook.Explorer explorer = app.ActiveExplorer();
Outlook.Namespace ns = app.GetNamespace("MAPI");
Outlook.Folder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
對於 VSTO 加載項,您可以使用ThisAddin
的Application
屬性:
Outlook.Explorer explorer = Application.ActiveExplorer();
Outlook.Namespace ns = app.GetNamespace("MAPI");
Outlook.Folder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
您可以在以下文章中閱讀有關Items
class 的Find / FindNext或Restrict方法的更多信息:
此外,您可能會發現AdvancedSearch方法很有幫助。 在 Outlook 中使用AdvancedSearch
方法的主要好處是:
AdvancedSearch
方法會在后台自動運行它。Restrict
和Find
/ FindNext
方法可以應用於特定的Items
集合(請參閱 Outlook 中文件夾 class 的 Items 屬性)。IsInstantSearchEnabled
屬性)。Stop
方法停止搜索過程。我不確定你為什么甚至訪問ActiveExplorer
- 你沒有使用它,如果 Outlook 之前沒有運行,則不會有任何打開的資源管理器(和檢查器),所以ActiveExplorer
無論如何都會返回 null。
另請記住,除非 Outlook 已經在運行,否則Application.Session
將是 null - 您需要先登錄。
第三,您沒有調用構造函數 - 那將是new Outlook.Application().Blah
(注意()
)。
將您的代碼更改為
Outlook.Application app = new Outlook.Application();
Outlook.Namespace session = app.GetNamespace("MAPI");
session.Logon();
Outlook.Folder inbox = session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.