Gintas K
Gintas K

Reputation: 1478

C#: Loop through unread email items in specific Outlook mailbox

I am very new to Outlook programming.

I have a mailbox let's say "ImportantMail" in my Outlook.

I am developing a windows form application which should find all the unread emails in that mailbox and loop through them accessing subject, sender & content.

Is there some simple method to achieve this?

I've tried following multiple tutorials I found, but it's too confusing for a fresher like me...

Thanks in advance!

Upvotes: 1

Views: 917

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

If ImportantMail mailbox is already in the current profile, retrieve that store from the Application.Session.Stores collection and call Store.GetDefaultFolder(olFolderInbox). If it is an Exchange delegate store that is not yet in the current profile, call Application.Session.CreateRecipient then Application.Session.GetSharedDefaultFolder(..., olFolderInbox).

Read the MAPIFolder.Items property, then use Items.Find/FindNext or Items.Restrict with a query like [Unread] = true.

Upvotes: 0

BugFinder
BugFinder

Reputation: 17858

I was working backwards in my case because I was moving mail items, so to make that work you need to go in reverse, however.. this really isnt a difficult thing to work out - I accounted for meeting requests etc..

for (int t = objFolder.Items.Count; t >= 1; t--)
{
    try
    {
        if (!(objFolder.Items[t] is MailItem)) continue;
        MailItem m = objFolder.Items[t];
        if (m.Unread) { do_stuff(); }
    }
    catch { }
}

Upvotes: 2

Related Questions