简体   繁体   中英

How do I parse ms outlook 2003 email body for a string i.e. keyword 'error'

I am trying to figure out how to parse ms outlook 2003 email body for a string using C# eg: read all the email in certain folder and search the email body for the word "error" in these emails.

Once this string error is found in the email body I would like to display the email and the error message.

I have the following code so far which basically just reads all the emails in the folder and displays all the information about the emails.

The issue I am having is actually trying to read into the mail item body in each individual email so I can search for the string.

I have been searching quite a while for this on web and have found code very similar to what I have below but none which shows you or if it is possible to read a folder containing emails, read through individual each email and search for the string 'errors', if this keyword is found in the email display the details from email ie email subject, error message etc.

Any help would be much appreciated.

Thanks a million. John.

using System;
using System.Reflection; 
using Outlook = Microsoft.Office.Interop.Outlook;

namespace AccessOutlook
{
     public class Class1
     {
          public static void Main(string[] args)
          {

               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;
               Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

               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["Docs"]; 
                    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.MailItem)subFolder.Items[i];

                         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;
               }

          }
     }
}

How about this:

 for(int i=1;i<=subFolder.Items.Count;i++)
{
    item = (Microsoft.Office.Interop.Outlook.MailItem)subFolder.Items[i];
    if(item.Body.Contains("errors"))
        {
            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); 
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM