-C # programming greatly improves the mail search capability of outlook!

Source: Internet
Author: User
Original article: [original]-C # programming greatly improves the mail search capability of outlook!

Have you ever encountered any problems when using outlook? Up to 18419 emails! Too many emails. It takes a lot of time to find an email. it is very urgent to find a solution to this problem. ms search can be used, but it is too heavy and does not support complex search functions such as logical search expressions. What should I do? Fortunately, I have the webus2.0 SDK, so I decided to develop a small tool named outlook searcher.

The outlook search wizard provides two functions:

1. Read Mail Information in outlook and create a full-text index;

2. Provides the search function and supports various complex logical expressions.

First, let's see how to read Outlook:

Reference COM components:

I reference version 9.4 here. Corresponding to outlook2010. then add the code to access 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); // get the default inbox sentbox = outlookns. getdefaultfolder (outlook. oldefaultfolders. olfoldersentmail); // get the default email }}

Outlook manages the inbox, sender, and email in the form of folder. generally, all emails we receive are in the "inbox", and all emails are in the "sent". Therefore, we can get the mail information from these two folders. for ease of use, I have created a mailinfo type to store the mail content to be indexed:

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();    }}

It also has the mapping function, which can be used in mailinfo and webus. document conversion. the index option is set for each field. now everything is ready. put the following code directly:

Create an index object first:

Iindexer indexaccessor = NULL ;... private void frmoutlooksearcher_load (Object sender, eventargs e ){... this. indexaccessor = new indexmanager (New mailanalyzer (); // use mailanalyzer as the analyzer this. indexaccessor. maxindexsize = int. maxvalue; // The index size is not limited. This. indexaccessor. minindexsize = int. maxvalue; // The index size is not limited. This. indexaccessor. mergefactor = int. maxvalue; // No merge ...}... private void indexproc () {indexaccessor. openornew (appdomain. currentdomain. basedirectory + @ "Index"); // place the index data in the "Index" folder of the running directory... // read outlook and add documents to index ...}

Read emails again and add index documents:

While (...) {// read inbox for (; inboxindx <= inbox. items. count; inboxindx ++ ){... this. initoutlookapp (); var item = inbox. items [inboxindx]; If (item is outlook. mailitem) // note that not every inbox item is mailitem, so you need to perform a type check. Otherwise, the program will be suspended and die there. {outlook. 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. )? String. empty: mailitem. to}; indexaccessor. add (mailinfo. todoc (); // Add the document to the index }...}... // read sentbox for (; sentboxindex <= sentbox. items. count; sentboxindex ++ ){...}}

Finally, put indexproc in the background thread to improve the user experience:

Private void frmoutlooksearcher_load (Object sender, eventargs e ){... this. indexaccessor = new indexmanager (New mailanalyzer (); // use mailanalyzer as the analyzer this. indexaccessor. maxindexsize = int. maxvalue; // The index size is not limited. This. indexaccessor. minindexsize = int. maxvalue; // The index size is not limited. This. indexaccessor. mergefactor = int. maxvalue; // No merge...Indexingtask= Task. Factory. startnew (this. indexproc );// Compile the index in the background thread}

OK, all done! The outlook search wizard supports the following search fields:

Field Type Description
Subject String Email Subject
Body String Body of the email, in HTML Format
Folder String Email directory, such as "inbox" and "sent"
From String Sender
To String Recipient
Conversationid String Session ID

 

 

 

 

 

 

 

By default, the outlook search wizard uses

Subject="{0}" OR Body="{0}"

For search, {0} is automatically replaced with the input keyword. however, if we enter a search expression, the outlook search Wizard will automatically switch to the advanced search mode and search using the expression entered by the user.

Here are several examples of Advanced Search:

// 1. Search for emails with the title containing "Zhang San" and the body containing "friend dinner": Subject = "Zhang San" & Body = "friend dinner"
// 2. Search for emails with the title "Zhang San" in the email box: folder = "[sent]" And subject = "Zhang San"
// 3. Search for emails with the title "hotfix": (hotfix and hotfixing will be searched) Subject wildcard "hotfix"

This is just some examples. With the support of webus2.0 SDK, the outlook search wizard can easily implement seven different types of searches and support complex logical search expressions, for details, see webus2.0 in action-search operation guide-(2 ).

In order to make the outlook search genie be considerate and easy to use, I have also designed some small functions, such as outlook connection interruption, automatic reconnection, minimized to tray, etc. Enjoy!

Download program | download source code

 Download related information and webus2.0 SDK:Continue with my code and share my happiness-webus2.0

-C # programming greatly improves the mail search capability of outlook!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.