开发者

Error while reading inbox items from outlook using C#

开发者 https://www.devze.com 2023-02-03 04:35 出处:网络
HI When i am trying to read data from inbox of outlook , i am getting the below error Unable to cast COM object of type \'System.__ComObject\' to interface type \'Microsoft.Office.Interop.Outlook.Po

HI

When i am trying to read data from inbox of outlook , i am getting the below error

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.PostItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063024-0000-0000-C000-000000000046}' failed due to the followin开发者_如何转开发g error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Below is my C# code

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;
}

Thanks in Advance


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);

Change this part as

 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 );

further refer

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

0

精彩评论

暂无评论...
验证码 换一张
取 消