Parsing emails in a C # mailing list

I am writing a program to read all my Outlook emails, eventually the search will be more specific, but for now I want to read all the emails in my inbox. I have code running that reads what I want up to 169 for some reason ...

namespace reademail { static class Program { public static Microsoft.Office.Interop.Outlook.Application myApp; public static void Main(string[] args) { // myApp = new Microsoft.Office.Interop.Outlook.Application(); //myApp.NewMailEx += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailExEventHandler(OutlookNewMailReceived); ReadMail(); } static void ReadMail() { Microsoft.Office.Interop.Outlook.Application app = null; Microsoft.Office.Interop.Outlook._NameSpace ns = null; Microsoft.Office.Interop.Outlook.MailItem item = null; Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; 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["Inbox"]; //folder.Folders[1]; also works Console.WriteLine("Folder Name: {0}, EntryId: {1}", inboxFolder.Name, inboxFolder.EntryID); Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString()); //System.IO.StreamWriter strm = new System.IO.StreamWriter("C:/Test/Inbox.txt"); for (int counter = 1; counter <= inboxFolder.Items.Count; counter++) { Console.Write(inboxFolder.Items.Count + " " + counter); item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[counter]; Console.WriteLine("Item: {0}", counter.ToString()); Console.WriteLine("Subject: {0}", item.Subject); Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString()); Console.WriteLine("Sendername: {0}", item.SenderName); Console.WriteLine("Body: {0}", item.Body); //strm.WriteLine(counter.ToString() + "," + item.Subject + "," + item.SentOn.ToShortDateString() + "," + item.SenderName); } //strm.Close(); } } } 

The cycle reads up to 169 letters, and then fails, it also starts reading letters by what seems like an arbitrary date ... I'm not sure what prevents it from reading all emails ...

 Folder Name: Inbox, EntryId: 000000003527EA8DB4FFC04EB6ABA4DE31CB4BA40100C6D3EBA DBDB57E438D0B53C5FB515CC50000660627C70000 Num Items: 1048 System.InvalidCastException: Unable to cast COM object of type 'System.__ComObje ct' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operatio n failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following er ror: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERF ACE)). at CallSite.Target(Closure , CallSite , Object ) at reademail.Program.ReadMail() in C:\Documents and Settings\DBubel\my docume nts\visual studio 2010\Projects\reademail\reademail\Program.cs:line 60 

Press any key to continue.,.

+4
source share
2 answers

I assume that you have items in your mailbox that don’t use the Microsoft.Office.Interop.Outlook.MailItem interface, and therefore the code crashes when passing through the loop and trying to do. One possible workaround for a solution if you use .NET4, because it supports dynamic, is not to throw the object, but rather pass it to the dynamic variable.

dynamic item = inboxFolder.Items[counter];

This worked for me because your code was having trouble processing meeting requests in the inbox.

Full modified code:

  namespace reademail { static class Program { public static Microsoft.Office.Interop.Outlook.Application myApp; public static void Main(string[] args) { // myApp = new Microsoft.Office.Interop.Outlook.Application(); //myApp.NewMailEx += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailExEventHandler(OutlookNewMailReceived); ReadMail(); } static void ReadMail() { Microsoft.Office.Interop.Outlook.Application app = null; Microsoft.Office.Interop.Outlook._NameSpace ns = null; //Microsoft.Office.Interop.Outlook.MailItem item = null; Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; 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["Inbox"]; //folder.Folders[1]; also works Console.WriteLine("Folder Name: {0}, EntryId: {1}", inboxFolder.Name, inboxFolder.EntryID); Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString()); //System.IO.StreamWriter strm = new System.IO.StreamWriter("C:/Test/Inbox.txt"); for (int counter = 1; counter <= inboxFolder.Items.Count; counter++) { Console.Write(inboxFolder.Items.Count + " " + counter); dynamic item = inboxFolder.Items[counter]; //item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[counter]; Console.WriteLine("Item: {0}", counter.ToString()); Console.WriteLine("Subject: {0}", item.Subject); Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString()); Console.WriteLine("Sendername: {0}", item.SenderName); Console.WriteLine("Body: {0}", item.Body); //strm.WriteLine(counter.ToString() + "," + item.Subject + "," + item.SentOn.ToShortDateString() + "," + item.SenderName); } //strm.Close(); } } } 

+4
source

If you only want MailItems , then you should verify that the item you are MailItem is a valid MailItem instead of assuming you have one. It can be CalendarItem , DocumentItem , etc. Which changes to olItemType . Your current code explicitly assumes that only MailItems in your inbox.

 item = inboxFolder.Items[counter] as Microsoft.Office.Interop.Outlook.MailItem; if (item != null) { .... } 

Your error indicates that you may have MailItem - but it may be a mail delivery report (Read Receipt, etc.). See this post for help with casting error. They suggest using the Outlook Table interface as a workaround and checking the Class object. This may be another option for you.

+1
source

Source: https://habr.com/ru/post/1401043/


All Articles