C# 从 Outlook 获取未读邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2055811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:04:29  来源:igfitidea点击:

Get unread Mails from Outlook

c#.netoutlook

提问by Phil

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

有什么方法可以将特定文件夹中的所有邮件放入我的应用程序中?

采纳答案by PRR

Check this link. Introduction to Outlook Programmingwill explain things more clearly.

检查此链接Outlook 编程简介将更清楚地解释事情。

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

常见的 Outlook 任务

回答by stuartd

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

有访问Outlook文件夹的一些例子在这里,其中一个有未读的邮件专门处理。

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

编辑:有一篇专门关于从 C# 访问文件夹的知识库文章,Programming samples that can reference items and folder in Outlook by using Visual C# .NET

To open another user's folder, use GetSharedDefaultFolder

要打开另一个用户的文件夹,请使用GetSharedDefaultFolder

回答by Dmitry Streblechenko

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

遍历文件夹中的所有项目是一个糟糕的主意,尤其是当您使用在线 Exchange 商店时。Items.Find/FindNext或者Items.Restrict是要走的路。

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:

Items.Restrict:

OutLook.Items items = oFolder.Items.Restict("[Unread] = true")
foreach (OutLook.MailItem mail in items)
{
  MessageBox.Show(mail.Subject);
}

回答by Arun Dhananjeyan

 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

我在上述解决方案中遇到了“COM_object”异常错误,更多信息请参考 这里