ASP.NET web page getting emails from outlook problem
We just launched a new task to retrieve emails from exchange server for different accounts using asp.net web application. I don't have such experience in past, but after searching online I found a code snippet which could communicate with outlook and 开发者_如何学JAVAfetch emails from there. However, there is an exception occurs every time I test the code which is :
"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)). "
Does anyone know the reason?
By the way, any suggestion helping on fetching emails from exchange server directly is highly appreciated!
My 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("user", "password", false, false);
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Folder Name:{0},EntryId:{1}", inboxFolder.Name, inboxFolder.EntryID);
sb.AppendFormat(" Num Items:{0}", inboxFolder.Items.Count.ToString());
Response.Write(sb);
for (int i = 1; i <= inboxFolder.Items.Count; i++)
{
item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i];//this is the exception happened line
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 (Exception)
{
throw;
}
finally
{
ns = null;
app = null;
inboxFolder = null;
}
Here goes:
With these lines...
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
...you're creating a reference to the user's Inbox. So far so good.
But then here...
for (int i = 1; i <= inboxFolder.Items.Count; i++)
{
item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i];
//other stuff...
}
...you're saying, "for every Item in the Inbox, assume that the Item is a PostItem."
According to MSDN, a PostItem:
Represents a post in a public folder that others may browse.
The user's inbox isn't going to be full of post items. It's going to contain MailItem objects representing the user's emails. That being the case, that line of code should probably be
item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[i];
Caveat: I don't know enough about Outlook's API to understand whether it's possible for there to be Outlook Item Objects other than MailItem objects in the Inbox, but I'd wager not. For reference, the full list of Outlook Item Objects is here.
Use as shown below:
// not PostItem
item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[i];
精彩评论