The easiest way is to use Mail.dll IMAP client search API:
List<long> uids = imap.Search().Where(
Expression.And(
Expression.Subject("Sales"),
Expression.HasFlag(Flag.Unseen)));
The above code searches for messages that contain word "Sales" in the email subject and are unseen.
Expression.Subject
finds emails that contain the specified string in the envelope's SUBJECT field.
Such search is performed entirely on the IMAP server side.
Alternative approach is to perform a client side search:
using (Imap imap = new Imap())
{
imap.ConnectSSL("imap.example.com");
imap.UseBestLogin("user", "password");
imap.SelectInbox();
List<long> uids = imap.Search(Flag.Unseen);
List<MessageInfo> infos = imap.GetMessageInfoByUID(uids);
List<long> result = new List<long>();
foreach (MessageInfo info in infos)
{
long uid = info.UID;
string subject = info.Envelope.Subject;
if (subject.Contains("Sales"))
result.Add(uid);
}
imap.Close();
}
In the sample above Imap.GetMessageInfoByUID
is used - it additionally downloads email structure, so you could access email attachments info as well.
If that is not needed you can use Imap.GetEnvelopeByUID
instead.
You can combine this approach with remembering largest uid you already processed, so all subsequent searches download data for new emails only:
https://www.limilabs.com/blog/get-new-emails-using-imap