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
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)
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.
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)
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")
)
);
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?
A lot of IMAP servers are buggy, so it wouldn't surprise me.
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?
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.
Alright thank you very much for your help