i try to read emails from inbox folder and i get the follwoing exception. I
use Asp .net 3.5, any one can help me
This is the code i used
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);
subFolder = inboxFolder.Folders["MySubFolderName"];
//folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}",
subFolder.Name, subFolder.EntryID);
Console.WriteLine("Num Items: {0}",
subFolder.Items.Count.ToString());
for (int i = 1; i <= subFolder.Items.Count; i++)
{
item =
(Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];// Here i got
that error
Console.WriteLine("Item: {0}", i.ToString());
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}",
item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
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;
}
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 following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))