从Outlook获取未读邮件

10

有没有办法将特定文件夹中的所有邮件获取到我的应用程序中?

4个回答

17

请查看这个链接Outlook编程介绍会更清楚地解释事情。

您可以循环遍历邮件项。 示例代码:

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

编辑: 引用其他文件夹

oFolder.Folders["Foldername"]

OutLook Code

常见的Outlook任务


是的,那就是我想要的!谢谢,但是有没有办法从Outlook中的另一个邮箱中获取收件箱中的项目? - Phil
1
循环遍历文件夹中的所有项目是一个糟糕的想法,特别是如果你正在与在线Exchange存储打交道。使用Items.Find/FindNext或Items.Restrict才是正确的方法。 - Dmitry Streblechenko

6

遍历文件夹中所有项目是一个糟糕的想法,特别是当你在与在线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:

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

昨天检索电子邮件的筛选器是什么?我尝试了这个 [SentOn] > '5/17/2017 4:21:33 PM',但似乎不起作用。它返回零条记录。 - karun_r
要么是那个,要么是[ReceivedTime]。 - Dmitry Streblechenko
是的,我在格式化日期时犯了一个小错误。谢谢。 - karun_r

1

0
 foreach (Object Unreadmail in folderItems)
            {
                if ((Unreadmail as Outlook.MailItem) != null && (Unreadmail as Outlook.MailItem).UnRead == true)
                {
                  //DO Your action Here
                }
            }

我在使用以上解决方案时遇到了“COM_object”异常错误,请参考此处获取更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接