使用C#从outlook中读取收件箱项时出错

时间:2011-01-10 09:34:28

标签: c# outlook

HI

当我尝试从Outlook的收件箱中读取数据时,我收到以下错误

无法将“System .__ ComObject”类型的COM对象强制转换为接口类型“Microsoft.Office.Interop.Outlook.PostItem”。此操作失败,因为由于以下错误,IID为“{00063024-0000-0000-C000-000000000046}”的接口的COM组件上的QueryInterface调用失败:不支持此类接口(HRESULT异常:0x80004002(E_NOINTERFACE))

以下是我的C#代码

Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.PostItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

try 
{
    app = new Microsoft.Office.Interop.Outlook.Application();
    ns = app.GetNamespace("MAPI");
    ns.Logon(null,null,false, false);

    inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); 
     Console.WriteLine("Folder Name: {0}, EntryId: {1}", inboxFolder.Name, inboxFolder.EntryID);
     Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString());
     Console.ReadLine();

     for (int i = 1; i <= inboxFolder.Items.Count; i++)
     {
         item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i];
         Console.WriteLine("Item: {0}", i.ToString());
         Console.WriteLine("Subject: {0}", item.Subject);
         Console.WriteLine("Categories: {0}", item.Categories);
         Console.WriteLine("Body: {0}", item.Body);
         Console.WriteLine("HTMLBody: {0}", item.HTMLBody);
     }
} 
catch (System.Runtime.InteropServices.COMException ex) 
{
    Console.WriteLine(ex.ToString());
}
finally
{
    ns = null;
    app = null;
    inboxFolder = null;
}

先谢谢

1 个答案:

答案 0 :(得分:1)

item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i];
     Console.WriteLine("Item: {0}", i.ToString());
     Console.WriteLine("Subject: {0}", item.Subject);


     Console.WriteLine("Categories: {0}", item.Categories);
     Console.WriteLine("Body: {0}", item.Body);
      Console.WriteLine("HTMLBody: {0}", item.HTMLBody);

将此部分更改为

 Microsoft.Office.Interop.Outlook.MailItem item =    (Microsoft.Office.Interop.Outlook.MailItem)subFolder.Items[i];

Console.WriteLine( "Item: {0}", i.ToString() );
Console.WriteLine( "Subject: {0}", item.Subject );
Console.WriteLine( "Categories: {0}", item.Categories );
Console.WriteLine( "Body: {0}", item.Body );
Console.WriteLine( "HTMLBody: {0}", item.HTMLBody );

进一步参考

http://www.dotnetspider.com/forum/160348-how-read-items-from-outlook-inbox-subfolders-C-NET.aspx