The easiest way is to use Mail.dll IMAP client search API:
List<long> uids = imap.Search().Where(
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())
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;
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
You can combine this approach with remembering largest uid you already processed, so all subsequent searches download data for new emails only: