如何从Outlook的子文件夹中提取邮件主题?

时间:2018-06-27 02:47:19

标签: c# outlook

我能够从Outlook根文件夹中提取邮件的主题。

Outlook.Application test_app = new Outlook.Application();
Outlook.NameSpace test_space = test_app.GetNamespace("MAPI");
Outlook.MAPIFolder test_folder = test_space.Folders["TanRong.Loo@infineon.com"].Folders["Test"];
foreach (Outlook.MailItem items in test_folder.Items)
{
    Console.WriteLine(items.Subject);
}

但是,当我尝试访问根目录“ Test”文件夹的子文件夹时,它会给我一条错误消息。

Outlook.Application test_app = new Outlook.Application();
Outlook.NameSpace test_space = test_app.GetNamespace("MAPI");
Outlook.MAPIFolder test_folder = test_space.Folders["TanRong.Loo@infineon.com"].Folders["Test"].Folders["Test1"];
foreach (Outlook.MailItem items in test_folder.Items)
{
    Console.WriteLine(items.Subject);
}

错误消息是:

Unhandled Exception: System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at outlook_to_excel.Program.Main(String[] args) in C:\Users\LooTanRo\Desktop\Outlook Export\CS\outlook_to_excel\outlook_to_excel\Program.cs:line 51

我做错了什么?请帮助我,谢谢。

1 个答案:

答案 0 :(得分:1)

您假设文件夹中只有MailItem个对象。您还可以具有ReportItemMeetingItem对象。 将循环更改为

foreach (object item in test_folder.Items)
{
  Outlook.MailItem mItem = item as Outlook.MailItem;
  if (mItem != null) 
  {
     ...
相关问题