MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

Search Condition not working Well...

Open nik0s100 opened this issue 6 years ago • 8 comments

I've been trying to figure out a method to search without getting a problem with Or statement. I always get 0 results when there are results with the search Statement

  • OS: Windows 10
  • .NET Framework: 4.6
  • MailKit Version: 2.8

Code that I use to search: SearchQuery.FromContains("test").And(SearchQuery.SentSince(sentAfter.Date)).Or(SearchQuery.SubjectContains("test").And(SearchQuery.SentSince(sentAfter.Date)));

The Results from Log Search:

C: A00000006 UID SEARCH OR (FROM test SENTSINCE 18-Sep-2020) (SUBJECT test SENTSINCE 18-Sep-2020)
S: * SEARCH
S: A00000006 OK SEARCH completed.

Isn't Supposed to do this Search, its missing one OR? OR (FROM test SENTSINCE 18-Sep-2020) OR (SUBJECT test SENTSINCE 18-Sep-2020)

nik0s100 avatar Sep 18 '20 20:09 nik0s100

No. The search query being sent by MailKit is correct. IMAP uses LISP-like syntax.

See https://tools.ietf.org/html/rfc3501#section-6.4.4 for the specification.

jstedfast avatar Sep 19 '20 02:09 jstedfast

On second thought, maybe I should break this down and explain what it is doing:

Your C# expression can be rewritten like this:

SearchQuery.Or (
    SearchQuery.And (
        SearchQuery.FromContains ("test"),
        SearchQuery.SentSince (sentAfter.Date)
    ),
    SearchQuery.And (
        SearchQuery.SubjectContains ("test"),
        SearchQuery.SentSince (sentAfter.Date)
    )
);

If you serialize that to a pseudo IMAP SEARCH expression, you get:

OR
    AND
        FROM test
        SENTSINCE 18-Sep-2020
    AND
        SUBJECT test
        SENTSINCE 18-Sep-2020

But the AND keyword does not exist in IMAP syntax, so you combine expressions meant to be ANDed together using parenthesis:

OR
    (
        FROM test
        SENTSINCE 18-Sep-2020
    )
    (
        SUBJECT test
        SENTSINCE 18-Sep-2020
    )

Now remove the newlines:

OR (FROM test SENTSINCE 18-Sep-2020) (SUBJECT test SENTSINCE 18-Sep-2020)

jstedfast avatar Sep 19 '20 03:09 jstedfast

FWIW, you can optimize your current search query like this:

var query = SearchQuery.And (
    SearchQuery.SentSince (sentAfter.Date),
    SearchQuery.Or (
        SearchQuery.FromContains ("test"),
        SearchQuery.SubjectContains ("test")
    )
);

jstedfast avatar Sep 19 '20 03:09 jstedfast

Thing is I want it to search for

Where FromContains test OR SubjectContains test. And for both of this search I want the date to be SentSince sentAfter.Date I've tried this but I always get bad results. If I just use one of them I get the right one

This works just fine: SearchQuery.SubjectContains("test").And(SearchQuery.SentSince(sentAfter.Date))

but if I use this I get 0 results: SearchQuery.SubjectContains("test").And(SearchQuery.SentSince(sentAfter.Date)).Or(SearchQuery.FromContains("test"))

But this works for some weird reason: SearchQuery.Or(SearchQuery.FromContains("test"), SearchQuery.SubjectContains("test").And(SearchQuery.SentSince(sentAfter.Date)))

Maybe the Imap Server I use cant handle many queries?

nik0s100 avatar Sep 19 '20 11:09 nik0s100

A lot of IMAP servers are buggy, so it wouldn't surprise me.

jstedfast avatar Sep 19 '20 12:09 jstedfast

Any idea on how to make this workable? Maybe I do 2 query searchs and stick the UIDS together? But 2 queries take longer time than 1 to finish right?

nik0s100 avatar Sep 19 '20 12:09 nik0s100

You could see if that works and if it does, you have found at least 1 potential solution.

Another solution might be to fetch the ENVELOPE info for every message and do client-side searching, but that will be hugely expensive unless you are already caching that info anyway.

jstedfast avatar Sep 19 '20 14:09 jstedfast

Alright thank you very much for your help

nik0s100 avatar Sep 19 '20 14:09 nik0s100