jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.21k stars 822 forks source link

How can I make a SearchQuery with more than one keyword? #1674

Closed xx7Ahmed7xx closed 11 months ago

xx7Ahmed7xx commented 11 months ago

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? }

jstedfast commented 11 months ago
SearchQuery query = null;
foreach (var word in badwords)
{
    subquery = SearchQuery.SubjectContains(badword);

    query = query != null ? query.Or(subquery) : subquery;
}
xx7Ahmed7xx commented 11 months ago

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);
}
xx7Ahmed7xx commented 11 months ago

@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.

xx7Ahmed7xx commented 11 months ago

@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#?

jstedfast commented 11 months ago

This behavior is up to the IMAP server. Clients have no control.

xx7Ahmed7xx commented 11 months ago

What are your suggestions for such thing? Also are there any reference for such thing? So I could refer to?

jstedfast commented 11 months ago

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.

xx7Ahmed7xx commented 11 months ago

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 ?

jstedfast commented 11 months ago

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.

xx7Ahmed7xx commented 11 months ago

I understand you point of view, thanks for the information actually. But I meant, how the syncing should be done? timers? interrupt? etc..

jstedfast commented 11 months ago

The Fetch() method allows you to get message data for a specific range of messages, either using indexes or uids

xx7Ahmed7xx commented 11 months ago

So you mean to download data for example first 10k if possible, and then query next 10k etc?

jstedfast commented 11 months ago

Yes