4

I want to search imap inbox with mailkit based on multiple condition like NotSeen & NotDeleted. I know the queries are receptively,if we do individual search query.

var uids = client.Inbox.Search(SearchQuery.NotSeen); 
var uids = client.Inbox.Search(SearchQuery.NotDeleted );   

but i need to put those two queries together & get all Uids based on the conditions. Any help would be appreciated.

KulOmkar
  • 263
  • 1
  • 17

3 Answers3

12

You can combine search queries using the And() and/or Or():

var uids = client.Inbox.Search (SearchQuery.NotSeen.And (SearchQuery.NotDeleted));

or

var uids = client.Inbox.Search (SearchQuery.And (SearchQuery.NotSeen, SearchQuery.NotDeleted));
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Thanks For the reply... great answer :) – aspdotnetdevelopertest Sep 27 '16 at 07:16
  • What if i need to serach/Get first 10 email base on SearchQuery? – Parth Akbari Mar 21 '17 at 07:07
  • @ParthAkbari You have 2 options. Either use the `Search` method as above, but just use the first 10 uids (assuming you want the default ordering)... or you can construct the `SearchQuery` the same as above, but then specify your own sort ordering and then call the [Sort](http://www.mimekit.net/docs/html/M_MailKit_Net_Imap_ImapFolder_Sort_1.htm) method instead (which returns the same results as `Search`, but in whatever order you ask for). For example: `var uids = folder.Sort (query, new OrderBy[] { OrderBy.ReverseArrival });` – jstedfast Mar 21 '17 at 13:41
2

This snippet adds an conditional search condition after the main query has been set

var query = MailKit.Search.SearchQuery.FromContains("anyone@gmail.com")
                            .And(MailKit.Search.SearchQuery.SubjectContains("Your Subject"));

query = query .And(MailKit.Search.SearchQuery.DeliveredAfter(DateTime.Parse("2020-02-28")));
tnorris
  • 21
  • 1
0

ok i found a way to manage it. Please suggest me if you have better way.

            var unread = client.Inbox.Search(SearchQuery.NotSeen);
            var Deleted = client.Inbox.Search(SearchQuery.Deleted);
            var Res  = unread.Except(Deleted).ToList();