问题描述
我正在尝试使用 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?
推荐答案
我遇到了同样的问题 - 我的解决方法只是创建一个 List
并循环遍历它.但请确保电子邮件不在子文件夹中,否则将无法找到.
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 检索所有邮件项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!