0 votes

How to get new 100 mail in all box in outlook

by (670 points)
i am asking Outlook.com new means new mail in inbox or other folder how to get 100 mail

1 Answer

0 votes

Outlook is an email program. I assume you have Outlook.com or Outlook365 in mind.

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.UseBestLogin("user@outlook.com", "password");

    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();

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.

by (298k points)
i am asking Outlook.com new means new mail in inbox or other folder how to get 100 mail
@Vilas This is what I assumed. So the answer is correct.
how to get all inbox mail by date range?
Use Search method (https://www.limilabs.com/blog/how-to-search-imap-in-net) and appropriate Expression methods (Expression.Since, Expression.SentSince, Expression.Before, Expression.SentBefore)