开发者

Get unread Mails from Outlook

开发者 https://www.devze.com 2022-12-16 01:22 出处:网络
Is there any way to get all mail from an specific开发者_开发百科 Folder into my Application?Check this link. Introduction to Outlook Programming will explain things more clearly.

Is there any way to get all mail from an specific开发者_开发百科 Folder into my Application?


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


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


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


 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

0

精彩评论

暂无评论...
验证码 换一张
取 消