Executing a search query in Outlook

Hello, I am wondering if it is possible to send a search query to Outlook 2010 from my WinForms application. That is, do not search the .PST file as I searched and found, I am trying to display the list of results in Outlook, as if I had entered the search box myself.

If possible, any sample code would be helpful. In addition, you can directly search all mail items compared to usually when you perform a search, it combs the current folder. Thanks.

+6
source share
1 answer

If you want to access Outlook data (for example, by mail), you need to add the COM link to the Microsoft Outlook XX object library

For Outlook, you can use COM interoperability . Open the Add Link dialog box and select the .NET tab, and then add the link to the Microsoft.Office.Interop.Outlook assembly.

enter image description here

Then be sure to add the namespace "Microsoft.Office.Interop.Outlook" to your usage suggestions.

Now you can create an instance of the Outlook application object:

Microsoft.Office.Interop.Outlook.Application outlook; outlook = new Microsoft.Office.Interop.Outlook.Application(); 

Let the request in your inbox be completed:

 MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderInbox); IEnumerable<MailItem> mail = folder.Items.OfType<MailItem>().Where(m => m.Subject == "Test").Select(m => m); 

You specify the folder you want to search as a parameter of the GetDefaultFolder (...) method. You can specify a folder other than the inbox.

  • olFolderSentMail
  • olFolderOutbox
  • olFolderJunk
  • ...

Check each possible value in MSDN:

Listing OlDefaultFolders

Stefan Cruysbergs has created an OutlookProvider component that acts as a wrapper for an Outlook application object. You can use LINQ to query this provider and retrieve data such as contacts, mail ... etc. Just download its code and check it out. That should be enough to get you started.

+8
source

Source: https://habr.com/ru/post/918778/


All Articles