Closed xx7Ahmed7xx closed 11 months ago
SearchQuery query = null;
foreach (var word in badwords)
{
subquery = SearchQuery.SubjectContains(badword);
query = query != null ? query.Or(subquery) : subquery;
}
Works as expected. Full code for future reference:
SearchQuery searchQuery;
string[] badWords = { "test", "some", "words", "here" };
foreach (var word in badWords)
{
var subQuery = SearchQuery.SubjectContains(word);
searchQuery = searchQuery != null ? searchQuery.Or(subQuery) : subQuery;
}
Also, here is a solution by Bard-Ai "I didn't test but seems the same":
// Define the list of words to search for
string[] words = new string[] { "word1", "word2", "word3" };
// Construct the search query using a foreach loop
var searchQuery = SearchQuery.All;
foreach (var word in words)
{
searchQuery &= SearchQuery.Contains(SearchQuery.Kind.Body, word);
}
@jstedfast Another possibility, What if I want to check for insensitive state of character (capital and small)? Edit: The input for SearchQuery.SubjectContains("text") does the job automatically and finds it whether in capital or small, thanks.
@jstedfast Please, I found something urgent here. This doesn't work, it is more like insensitive EXACT matching, if for example: "This is some STRing thing to search in the body of the email" and I search for "sTr" it won't work, it needs to have "sTr" or "str" or "STR" separated from anything else, how to make it like Contains() method in C#?
This behavior is up to the IMAP server. Clients have no control.
What are your suggestions for such thing? Also are there any reference for such thing? So I could refer to?
According to the spec, the server is supposed to match case-insensitive:
https://datatracker.ietf.org/doc/html/rfc3501#section-6.4.4
If the IMAP server doesn't do that, then your only option is to Fetch() the Envelope data and do string matches manually which will be very expensive to do, but there's no other choice.
Then again, if you are writing a mail client, you'll probably want this info cached locally anyway in which case you wouldn't need to do server-side search at all.
How should the caching be done? You mean to download all messages data locally? What if the client loads in a very old email account with thousands of messages.. I know that in this library I can download parts of message (but not very clear to me).. But still, how could I organize it for an efficient way to load it through Mailkit.NET ?
It's not that difficult, it just takes time to implement. I'd probably use SQLite to store the data, but you could use System.Text.Json. In fact that might be easier.
You just have to design it to sync the data from the server incrementally.
I understand you point of view, thanks for the information actually. But I meant, how the syncing should be done? timers? interrupt? etc..
The Fetch() method allows you to get message data for a specific range of messages, either using indexes or uids
So you mean to download data for example first 10k if possible, and then query next 10k etc?
Yes
So I have this code:
var query = SearchQuery.SubjectContains ("Search Text1").Or (SearchQuery.SubjectContains ("Search Text2")); var uids = client.Inbox.Search (query);
However, this searches for static strings. If I want to create this behavior in a for loop or something, How is that possible? Example:foreach (var word in badWords) { searchQuery = // What here? }