Unable to cast COM object - Microsoft outlook & C#

Hi

I am looping through my inbox to locate undeliverable emails.

When it comes to the undeliverable email it throws a com error

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

this used to work on another machine.

What type of email is the undeliverable email (I know its not a MailItem or PostItem)

Code is below.

 Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._NameSpace oNS = (Microsoft.Office.Interop.Outlook._NameSpace)oApp.GetNamespace("MAPI");
            oNS.Logon(Missing.Value, Missing.Value, false, true);
Microsoft.Office.Interop.Outlook.MAPIFolder oFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Items items = oFolder.Items;

foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
{
       subject = mail.Subject.Trim();
       body = mail.Body.Trim();

       // Check if subject contains "Undeliverable"
       if (subject.IndexOf("Undeliverable:") != -1)
       {
                // do stuff here
       }

}

Open in new window


any help is appreciated
elmbrookAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

elmbrookAuthor Commented:
Never mind...
It is a ReportItem.
New code below

 Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook._NameSpace oNS = (Microsoft.Office.Interop.Outlook._NameSpace)oApp.GetNamespace("MAPI");
            oNS.Logon(Missing.Value, Missing.Value, false, true);
            Microsoft.Office.Interop.Outlook.MAPIFolder oFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            Microsoft.Office.Interop.Outlook.Items items = oFolder.Items;


            #endregion


            foreach (object mailitem in items)
            {

                if (mailitem is Microsoft.Office.Interop.Outlook.ReportItem)
                {
                    Microsoft.Office.Interop.Outlook.ReportItem mail = (Microsoft.Office.Interop.Outlook.ReportItem)mailitem;
                    subject = mail.Subject.Trim();
                    body = mail.Body.Trim();


                    // Check if subject contains "Undeliverable"
                    if (subject.IndexOf("Undeliverable:") != -1)
                    {
                       // do somthing

                      }
                }

                
                if (mailitem is Microsoft.Office.Interop.Outlook.MailItem)
                {
                    Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)mailitem;
                    // do something with mailitem

                    subject = mail.Subject.Trim();
                    body = mail.Body.Trim();


                    // Check if subject contains "Undeliverable"
                    if (subject.IndexOf("Undeliverable:") != -1)
                    {
                       // do somthing
                    }
                }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Since the a folder can contain more than just mail items, i would check to see if the item is a mail item and then cast; e.g. -
using System;
using System.Linq;
using Microsoft.Office.Interop.Outlook;

namespace EE_Q28735759
{
	class Program
	{
		static Application oApp = new Application();
		static NameSpace oNS = oApp.GetNamespace("MAPI");
		static MAPIFolder inbox = oNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

		static void Main(string[] args)
		{
			Items items = inbox.Items;
			foreach (var item in items)
			{
				if (item is MailItem)
				{
					MailItem mail = item as MailItem;
					if (mail.Subject != null && (mail.Subject.Contains("Undeliverable:") || mail.Subject.Contains("Mail delivery failed")))
						Console.WriteLine("{0}", mail.Subject);
				}
			}
			Console.WriteLine("Finished checking mail items in inbox");
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output for me -Capture.JPG-saige-
elmbrookAuthor Commented:
Thanks Saige

I was heading down that path already.

The issue was the undelivered email is not a MailItem so would never have been picked up(under MailItems), and that is the email I needed to process.

I figured out that the undelivered email is actually a ReportItem and process that separately as shown in my second posting.

Thanks for the post, great minds think alike!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.