【问题标题】:Error while reading inbox items from outlook using C#使用 C# 从 Outlook 读取收件箱项目时出错
【发布时间】:2011-06-06 10:19:51
【问题描述】:

当我尝试从 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;
}

提前致谢

【问题讨论】:

  • 你的收件箱里有约会等吗?

标签: c# outlook


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 2011-10-14
    • 1970-01-01
    • 2021-09-12
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    相关资源
    最近更新 更多