1.
Outlook is an email program. I assume you have Outlook.com or Outlook365 in mind.
2.
Next question is what does "new" mean to you?
- It can either be unseen emails (those that don't have \SEEN flag set)
- or those that you haven't processed the last time you checked.
You can find both those situations described in this two articles:
Receive unseen emails using IMAP
Get new emails using IMAP
Getting unseen email is easy - you just need to perform Flag.Unseen IMAP search. As search returns only uids it is quite fast you can limit the results on the client side using Skip method.
using(Imap imap = new Imap())
{
imap.ConnectSSL("imap-mail.outlook.com");
imap.UseBestLogin("user@outlook.com", "password");
imap.SelectInbox();
List<long> uids = imap.Search(Flag.Unseen);
List<long> last100 = uids.Skip(uids.Count - 100).ToList();
foreach (long uid in last100 )
{
var eml = imap.GetMessageByUID(uid);
IMail email = new MailBuilder().CreateFromEml(eml);
string subject = email.Subject;
string text = email.GetBodyAsText();
}
imap.Close();
}
Now, there are techniques to limit the IMAP search result count on the server side (specifying the Expression.UID range), but in most cases this is an overkill. Search in most cases is fast enough.
When getting 'new' emails you must save the highest uid you processed on last run, you can find the code in Get new emails using IMAP article.
new [...] mail in all box
Unfortunately IMAP protocol's SEARCH command doesn't support searching through all email folders. Although there exists ESEARCH command, it is not supported by most servers. The only solution is to treat each folder individually.
Some email providers like Gmail create virtual folder 'All mail', that contains all emails, which can be selected for searching.