Outlook.MailItem Issues
-
Hello All, I want to read all mail of one folder from outlook and here is code at which line i am getting error....
Outlook.Items MailItems;
MailItems = .......
for (int i = MailItems.Count; i >= 1; i += -1)
{
Outlook.MailItem mitem;
mitem = (Outlook.MailItem)MailItems.Item(i); //// I AM GETTING error here....
}When casting getting below exception.. Tried lot from Google and other sites not getting appropriate solution. Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
Thanks, Sun Rays To get something you must have to try once. My Articles
-
Hello All, I want to read all mail of one folder from outlook and here is code at which line i am getting error....
Outlook.Items MailItems;
MailItems = .......
for (int i = MailItems.Count; i >= 1; i += -1)
{
Outlook.MailItem mitem;
mitem = (Outlook.MailItem)MailItems.Item(i); //// I AM GETTING error here....
}When casting getting below exception.. Tried lot from Google and other sites not getting appropriate solution. Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
Thanks, Sun Rays To get something you must have to try once. My Articles
-
Were you able to figure this out? I am having the smae problem on undeliverable emails that are bounced back to the inbox. thanks dennis
The below section should assist anyone attempting to use the MailItem object. The key to this code not throwing exceptions is that the items collection may contain more than just MailItems and therefore the following code allows you to retrieve just the MailItems from the collection.
//create an instance of Outlook
Outlook.Application olApp = new Outlook.Application();
//create a MAPIFolder object pointed to the default inbox
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)olApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
//create an Items collection and pass in the inbox items
Outlook.Items Inboxitems = inBox.Items;//loop through the items that are MailItem and collect the size of the items
//to get the total size of the inbox
foreach (Outlook.MailItem item in Inboxitems.OfType())
{
InboxSize = InboxSize + item.Size;
}Obviously once you have a handle to the individual Mailitem you can access all of it's properties. Happy Coding :)