10

Is there any way to get all mail from an specific Folder into my Application?

Phil
  • 507
  • 2
  • 5
  • 7

4 Answers4

17

Check this link. Introduction to Outlook Programming will explain things more clearly.

You could loop through the mailitems. Sample code

using System.Runtime.InteropServices;
using OutLook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

    OutLook.Application oApp;
             OutLook._NameSpace oNS;
             OutLook.MAPIFolder oFolder;
             OutLook._Explorer oExp;

             oApp = new OutLook.Application();
             oNS = (OutLook._NameSpace)oApp.GetNamespace("MAPI");
             oFolder = oNS.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderInbox);
             oExp = oFolder.GetExplorer(false);
             oNS.Logon(Missing.Value, Missing.Value, false, true);

        OutLook.Items items = oFolder.Items;
        foreach (OutLook.MailItem mail in items)
                        {

                            if (mail.UnRead == true)
                            {
                        }
        }

Edit: Reference other folders

oFolder.Folders["Foldername"]

OutLook Code

Common Outlook tasks

Community
  • 1
  • 1
PRR
  • 1,190
  • 7
  • 13
  • Yeah thats what i want! Thanks, but is there a way to get the Items of the Inbox from another Mailbox wich is in Outlook? – Phil Jan 13 '10 at 12:13
  • 1
    Looping through all items in a folder is a terrible idea, especially if you are working against an online Exchange store. Items.Find/FindNext or Items.Restrict is the way to go. – Dmitry Streblechenko Sep 04 '15 at 03:43
6

Looping through all items in a folder is a terrible idea, especially if you are working against an online Exchange store. Items.Find/FindNext or Items.Restrict is the way to go.

Find/FindNext:

OutLook.Items items = oFolder.Items;
OutLook.MailItem mail = items.Find("[Unread] = true");
while (mail != null)
{
  MessageBox.Show(mail.Subject);
  mail = items.FindNext();
}

Items.Restrict:

OutLook.Items items = oFolder.Items.Restict("[Unread] = true")
foreach (OutLook.MailItem mail in items)
{
  MessageBox.Show(mail.Subject);
}
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
1

There's some examples of accessing Outlook folders here, one of which deals specifically with unread mail.

Edit: There's a KB article specifically about accessing folders from C#, Programming samples that can reference items and folders in Outlook by using Visual C# .NET

To open another user's folder, use GetSharedDefaultFolder

stuartd
  • 70,509
  • 14
  • 132
  • 163
0
 foreach (Object Unreadmail in folderItems)
            {
                if ((Unreadmail as Outlook.MailItem) != null && (Unreadmail as Outlook.MailItem).UnRead == true)
                {
                  //DO Your action Here
                }
            }

I have experienced "COM_object" exception error with above solutions, More info please refer here