標籤:style blog http color io os 使用 ar for
原文:[原創] - C#編程大幅提高OUTLOOK的郵件搜尋能力!
使用OUTLOOK, 你有沒有遇到過的問題? 多達18419封郵件! 太多了, 每次想找一個郵件都非常耗時, 想辦法解決這個問題成了一件非常緊迫的事情. 利用MS Search當然可以, 但是它太heavy了, 而且不支援如邏輯搜尋運算式等複雜尋找功能, 怎麼辦? 幸運的是我有WEBUS2.0 SDK, 於是我決定自己開發一個名為Outlook Searcher (Outlook搜尋精靈) 的小工具.
Outlook搜尋精靈主要包含兩個功能:
1. 讀取Outlook中的郵件資訊並建立全文索引;
2. 提供搜尋功能, 支援各種複雜的邏輯運算式.
先看看如何讀取Outlook:
引用COM組件:
我這裡引用的是9.4版本. 對應Outlook2010. 然後添加訪問Outlook的代碼:
using Outlook = Microsoft.Office.Interop.Outlook;...Outlook.Application OutlookApp;Outlook.NameSpace OutlookNS;Outlook.MAPIFolder Inbox;Outlook.MAPIFolder Sentbox;...void InitOutlookApp(){ if (OutlookApp == null) { OutlookApp = new Outlook.Application(); OutlookNS = OutlookApp.GetNamespace("MAPI"); Inbox = OutlookNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); //擷取預設的收件匣 Sentbox = OutlookNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail); //擷取預設的已發郵件 }}
Outlook以Folder的方式來管理收件匣, 寄件匣, 已發郵件等. 一般情況下, 我們接收的郵件都在"收件匣"中, 發出的郵件都在"已發郵件"中, 因此我們從這兩個檔案夾中擷取郵件資訊. 為了更加方便使用, 我建立了一個MailInfo類型來存放需要索引的郵件內容:
public class MailInfo{ public string EntryId { get; set; } public string Folder { get; set; } public string From { get; set; } public string Subject { get; set; } public string ConversationId { get; set; } public string Body { get; set; } public string To { get; set; } public Document ToDoc() { var doc = new Document(); doc.Fields.Add(new Field("EntryId", this.EntryId, Webus.Documents.FieldAttributes.None)); doc.Fields.Add(new Field("Folder", this.Folder, Webus.Documents.FieldAttributes.Index)); doc.Fields.Add(new Field("From", this.From, Webus.Documents.FieldAttributes.Index)); doc.Fields.Add(new Field("Subject", this.Subject, Webus.Documents.FieldAttributes.AnalyseIndex)); doc.Fields.Add(new Field("ConversationId", this.ConversationId, Webus.Documents.FieldAttributes.Index)); doc.Fields.Add(new Field("Body", this.Body, Webus.Documents.FieldAttributes.AnalyseIndex)); doc.Fields.Add(new Field("To", this.To, Webus.Documents.FieldAttributes.Index)); return doc; } public MailInfo() { } public MailInfo(Document doc) { this.EntryId = doc.GetField("EntryId").Value.ToString(); this.Folder = doc.GetField("Folder").Value.ToString(); this.From = doc.GetField("From").Value.ToString(); this.Subject = doc.GetField("Subject").Value.ToString(); this.ConversationId = doc.GetField("ConversationId").Value.ToString(); this.Body = doc.GetField("Body").Value.ToString(); this.To = doc.GetField("To").Value.ToString(); }}
它還兼具了Mapping的功能, 能夠在MailInfo和Webus.Document之間進行轉換. 並且為每個欄位都設定了索引選項. 現在一切就緒, 只欠東風了. 廢話少說, 直接上代碼:
先建立索引對象:
IIndexer IndexAccessor = null;...private void frmOutlookSearcher_Load(object sender, EventArgs e){ ... this.IndexAccessor = new IndexManager(new MailAnalyzer()); //用MailAnalyzer作為分析器 this.IndexAccessor.MaxIndexSize = int.MaxValue; //索引大小無限制 this.IndexAccessor.MinIndexSize = int.MaxValue; //索引大小無限制 this.IndexAccessor.MergeFactor = int.MaxValue; //不做merge ...}...private void IndexProc(){ IndexAccessor.OpenOrNew(AppDomain.CurrentDomain.BaseDirectory + @"Index"); //索引資料放在運行目錄的"Index"檔案夾裡面 ... //讀取outlook, 添加文檔到索引 ...}
再迴圈讀取郵件並添加索引文檔:
while(...){ //先讀取inbox for (; InboxIndx <= Inbox.Items.Count; InboxIndx++) { ... this.InitOutlookApp(); var item = Inbox.Items[InboxIndx]; if (item is Outlook.MailItem) //注意, 並非每個inbox的item都是mailItem, 因此要做個類型檢查, 否則程式會掛起, 死在那兒. { Outlook.MailItem mailItem = item as Outlook.MailItem; var mailInfo = new MailInfo() { EntryId = string.IsNullOrEmpty(mailItem.EntryID) ? string.Empty : mailItem.EntryID, From = string.IsNullOrEmpty(mailItem.SenderEmailAddress) ? string.Empty : mailItem.SenderEmailAddress, ConversationId = string.IsNullOrEmpty(mailItem.ConversationID) ? string.Empty : mailItem.ConversationID, Subject = string.IsNullOrEmpty(mailItem.Subject) ? string.Empty : mailItem.Subject, Body = string.IsNullOrEmpty(mailItem.HTMLBody) ? string.Empty : mailItem.HTMLBody, Folder = string.IsNullOrEmpty(Inbox.Name) ? string.Empty : Inbox.Name, To = string.IsNullOrEmpty(mailItem.To) ? string.Empty : mailItem.To }; IndexAccessor.Add(mailInfo.ToDoc()); //添加文檔到索引 } ... } ... //再讀取sentbox for (; SentboxIndex <= Sentbox.Items.Count; SentboxIndex++) { ... }}
最後將IndexProc放到後台線程中運行來提高使用者體驗:
private void frmOutlookSearcher_Load(object sender, EventArgs e){ ... this.IndexAccessor = new IndexManager(new MailAnalyzer()); //用MailAnalyzer作為分析器 this.IndexAccessor.MaxIndexSize = int.MaxValue; //索引大小無限制 this.IndexAccessor.MinIndexSize = int.MaxValue; //索引大小無限制 this.IndexAccessor.MergeFactor = int.MaxValue; //不做merge ... IndexingTask = Task.Factory.StartNew(this.IndexProc); //在後台線程編製索引}
OK, 大功告成! Outlook搜尋精靈支援如下搜尋欄位:
| 欄位 |
類型 |
描述 |
| Subject |
string |
郵件標題 |
| Body |
string |
郵件內文, HTML格式 |
| Folder |
string |
郵件所屬目錄, 比如"收件匣", "已發郵件"等 |
| From |
string |
寄件者 |
| To |
string |
收件者 |
| ConversationId |
string |
會話ID |
預設情況下, Outlook搜尋精靈會使用
Subject="{0}" OR Body="{0}"
進行搜尋, {0}會被自動替換成輸入的關鍵詞. 但是如果我們輸入的本身就是一個搜尋運算式, 那麼Outlook搜尋精靈會自動切換成進階搜尋模式, 用使用者輸入的運算式進行搜尋.
列舉幾個進階搜尋的例子:
//1. 搜尋標題含有"張三"並且本文含有"朋友聚餐"的郵件:Subject="張三" & Body="朋友聚餐"
//2. 在已發郵件中搜尋標題中含有"張三"的郵件:Folder="[已發郵件]" AND Subject="張三"
//3. 搜尋標題包含"Hotfix"的郵件: (hotfix和hotfixing都會被搜尋到)Subject WILDCARD "hotfix"
這隻是部分例子, 有了WEBUS2.0 SDK的支援, Outlook搜尋精靈可以輕鬆實現7種不同類型的搜尋, 並且支援複雜的邏輯搜尋運算式, 具體請看 WEBUS2.0 In Action - 搜尋操作指南 - (2).
為了讓Outlook搜尋精靈根據體貼好用, 我還設計了一些小功能, 比如Outlook串連中斷自動重連, 最小化到托盤等. enjoy吧!
下載程式 | 下載原始碼
相關資訊及WEBUS2.0 SDK下載:繼續My Code,分享我的快樂 - WEBUS2.0
- C#編程大幅提高OUTLOOK的郵件搜尋能力!