为什么我无法检索使用互操作的Outlook中的所有MailItems? [英] Why can't I retrieve ALL MailItems using interop outlook?

查看:114
本文介绍了为什么我无法检索使用互操作的Outlook中的所有MailItems?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Microsoft.Office.Interop.Outlook检索我的Outlook收件箱的邮件。这是我的代码:

I'm trying to use Microsoft.Office.Interop.Outlook to retrieve emails from my Outlook inbox. This is my code:

  Application app = new Application();
  NameSpace ns = app.Session;
  MAPIFolder inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
  Items items = inbox.Items;
  foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
        {
            if (mail as MailItem != null)
            {
                Console.WriteLine(mail.Subject.ToString());
                Console.WriteLine(mail.Body.ToString());
                Console.ReadKey();
             }
        }

当我这样做,它的工作原理 - 样的。这只能说明一个电子邮件。应该有三个。它显示的电子邮件是最古老的一个在那里......我为什么不能够得到所有三个?是否有除的MailItem一些其他类型的邮件,这将是我的收件箱?

When I do this, it works--sort of. It only shows one email. There should be three. The email it's showing is the oldest one in there... why wouldn't I be able to get all three? Is there some other type of mail besides MailItem that would be in my inbox?

推荐答案

我有同样的确切问题 - 我的解决方法是只创建一个列表<的MailItem> 和环路通过。确保电子邮件不是在子文件夹虽然,否则他们将不会被发现。

I had this same exact problem - My workaround was just to create a List<MailItem> and loop through that. Make sure the emails aren't in subfolders though, otherwise they won't be found.

Outlook.Application app = new Outlook.Application();
Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

List<MailItem> ReceivedEmail = new List<MailItem>(); 
foreach (Outlook.MailItem mail in emailFolder.Items)               
        { ReceivedEmail.Add(mail); }

foreach (MailItem mail in ReceivedEmail)
{
    //do stuff
}

这篇关于为什么我无法检索使用互操作的Outlook中的所有MailItems?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆