Outlook 郵件動輒上萬封,內建搜索慢、不支援邏輯表達式(AND/OR/WILDCARD)。MS Search 過重且功能受限。
兩大功能:
using Outlook = Microsoft.Office.Interop.Outlook;
Outlook.Application _app;
Outlook.NameSpace _ns;
Outlook.MAPIFolder _inbox, _sentbox;
void Init() {
_app = new Outlook.Application();
_ns = _app.GetNamespace("MAPI");
_inbox = _ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
_sentbox = _ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
}
public class MailInfo {
public string EntryId, Folder, From, Subject, ConversationId, Body, To;
public Document ToDoc() {
var doc = new Document();
doc.Fields.Add(new Field("EntryId", EntryId, FieldAttributes.None));
doc.Fields.Add(new Field("Folder", Folder, FieldAttributes.Index));
doc.Fields.Add(new Field("From", From, FieldAttributes.Index));
doc.Fields.Add(new Field("Subject", Subject, FieldAttributes.AnalyseIndex));
doc.Fields.Add(new Field("ConversationId", ConversationId, FieldAttributes.Index));
doc.Fields.Add(new Field("Body", Body, FieldAttributes.AnalyseIndex));
doc.Fields.Add(new Field("To", To, FieldAttributes.Index));
return doc;
}
// 反向映射構造函數略
}
IIndexer _indexer = new IndexManager(new MailAnalyzer());
_indexer.MaxIndexSize = int.MaxValue;
_indexer.MergeFactor = int.MaxValue; // 不自動合併
Task.Factory.StartNew(() => {
_indexer.OpenOrNew(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index"));
// 遍歷 Inbox Items
for (int i = 1; i <= _inbox.Items.Count; i++) {
var item = _inbox.Items[i];
if (item is Outlook.MailItem mail) {
var info = new MailInfo { /* 填充屬性 */ };
_indexer.Add(info.ToDoc());
}
}
// 同理處理 Sentbox
_indexer.Commit();
});
注意:Items 索引從 1 開始;非 MailItem 需類型檢查否則掛起。
默認模式:Subject="{kw}" OR Body="{kw}"。輸入含運算符則進入高級模式:
Subject="張三" & Body="朋友聚餐"(AND)Folder="[已發郵件]" AND Subject="張三"Subject WILDCARD "hotfix"(通配符,匹配 hotfix/hotfixing)支援字段:EntryId、Folder、From、Subject、ConversationId、Body、To。