Link to home
Start Free TrialLog in
Avatar of SirReadAlot
SirReadAlot

asked on

outlook programming in c#

Hi experts,
just wondering if anyone knows how to program outlook via c#.
I have built a console application which arranges my outlook messages into folders. for exmple if the its an
unopened email msg, the console app leaves that particular msg, else every read msg gets moved to my junk folder.

What I want to do now is be able to find every replied email with no attachments to the bin folder.

How do i accomplish this. Any relavant articles or code will be nice
thanks


pls add reference to  Microsoft.Office.Interop.Outlook; thanks

This is what i have done so far. it works  well. see code below
================================================
using System;
using Microsoft.Office.Interop.Outlook;

namespace EmailSorter
{
      /// <summary>
      /// Summary description for Loader.
      /// </summary>
      public class Loader
      {

            private static Application oApp;
            private static NameSpace oNS;
                  
            private static Recipient oUser;
                  
            private static MAPIFolder oFolder;
            private static MAPIFolder oReadNotActionedDestinationFolder;
            private static MAPIFolder oReadDestinationFolder;
            private static MAPIFolder oSentFolder;
            private static MAPIFolder oDeletedFolder;
      

            [STAThread]
            static void Main(string[] args)
            {
                  

                  Console.WriteLine("outlook inbox manager");
                  Console.WriteLine("  In progress");
                  
                  Console.WriteLine("    Loading outlook folders");
                  inistialiseFolders();
                  
                  //MailItem oMsg = (MailItem)oItems.GetFirst();
                  int iCount = 0;
                  int iNewCount = 1;
                  
                  Console.WriteLine("    Processing mail items");

                  while (iCount != iNewCount)
                  {
                  
                        Items oItems = oFolder.Items;
                        iCount = oItems.Count;
                        for (int i=1; i<=iCount; i++)
                        {
                              try
                              {
                                    MailItem oMsg = (MailItem)oItems[i];

                                    Console.WriteLine("      Mail item: " + oMsg.Subject);

                                    if (!oMsg.UnRead && hasBeenActioned(oMsg.Subject))
                                    {
                                          oMsg.Move(oReadDestinationFolder);
                                          Console.WriteLine("         moved to junk folder");
                                    }
                              

                                    if (!oMsg.Sent && hasBeenSent())
                                    {
                                          oMsg.Move(oDeletedFolder);
                                          Console.WriteLine("         Deleted");
                                    }

                                    if (!oMsg.UnRead)
                                    {
                                          oMsg.Move(oReadNotActionedDestinationFolder);
                                          Console.WriteLine("         task created");
                                    }
                                    else
                                    {
                                          Console.WriteLine("         not touched");
                                    }
                                                
                              }
                              catch (System.InvalidCastException ice)
                              {
                                    Console.WriteLine("      Not an item of mail");
                              }
                              catch {}
                        }
                        iNewCount = oItems.Count;
                  }      
                  Console.WriteLine("Completed");
                  Console.WriteLine("Press enter to exit application");
                  Console.Read();
            }

            private static bool hasBeenActioned (string mailSubject)
            {
                  bool retval = false;
                  for (int i=1; i<=oSentFolder.Items.Count && retval==false; i++)
                  {
                        try
                        {
                              MailItem msg = (MailItem)oSentFolder.Items[i];
                              if (msg.Subject == mailSubject)
                              {
                                    retval = true;
                              }
                        }
                        catch{}
                  }
                  return retval;
            }
            

            private static bool hasBeenSent()
            {
                  bool retval = false;
                  for (int i=1; i<=oDeletedFolder.Items.Count && retval==false; i++)
                  {
                        try
                        {
                              MailItem msg = (MailItem)oDeletedFolder.Items[i];
                              if (msg.Sent)
                              {
                                    retval = true;
                              }
                        }
                        catch{}
                  }
                  return retval;
            }


            private static void inistialiseFolders()
            {
                  oApp = new ApplicationClass();
                  oNS = oApp.GetNamespace("MAPI");
                  
                  oUser = oNS.CreateRecipient("Azunma, Chi");
                  
                  oFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderInbox);
                  oReadNotActionedDestinationFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderTasks);
                  oReadDestinationFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderJunk);
                  oSentFolder = oNS.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
                  oDeletedFolder = oNS.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
            }

                    
      }
}

==================================================
pl
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Optimization's first:

          private static bool hasBeenSent()
          {
               bool retval = false;
               foreach (MailItem msg in oDeletedFolder.Items)
                  if (msg.Sent)
                     retval = true;

               return retval;
          }

Now, you want to find email messages that don't have attachments (msg.Attachments.Count > 0)?

What version of Outlook are you using (I have 2003)?


Bob
Avatar of SirReadAlot
SirReadAlot

ASKER

thanks for comming on board

I have 2003
Boy, you just zeroed right in on that one question.  *GRIN*  

There was another one in there:

              Now, you want to find email messages that don't have attachments (msg.Attachments.Count > 0)?

Bob
Slight modification:

         private static bool hasBeenSent()
          {
               bool retval = false;
               foreach (MailItem msg in oDeletedFolder.Items)
                  if (msg.Sent)
                  {
                     retval = true;
                     break;
                  }

               return retval;
          }

I forgot the 'break' statement.

Bob
yep,
     Now, you want to find email messages that don't have attachments (msg.Attachments.Count > 0)?

i guess thats the next step.
Try this:

        private static ArrayList GetEmailsWithAttachments(MAPIFolder folder)
          {
               ArrayList list = new ArrayList;
               foreach (MailItem msg in folder.Items)
                  if (msg.Attachments.Count > 0)
                     list.Add(msg);

               return list;
          }

Bob
okay, will try
thanks,

Got this error,
namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?)


private static ArrayList GetEmailsWithAttachments(MAPIFolder folder)
            {
                  ArrayList list = new ArrayList();
                  foreach (MailItem msg in folder.Items)
                        if (msg.Attachments.Count > 0)
                              list.Add(msg);

                  return list;
            }

namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?)
sorry solve it with
using System.Collections;
What I want to do now is be able to find every replied email with no attachments and move it to the bin folder.

did the code work for you

using System;
using Microsoft.Office.Interop.Outlook;
using System.Collections;


namespace EmailSorter
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Loader
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            
            
            
            private static Application oApp;
            private static NameSpace oNS;
            private static Recipient oUser;
                  
            private static MAPIFolder oFolder;
            private static MAPIFolder oReadNotActionedDestinationFolder;
            private static MAPIFolder oReadDestinationFolder;
            private static MAPIFolder oSentFolder;
            private static MAPIFolder oDeletedFolder;

                        
            
            [STAThread]
            static void Main(string[] args)
            {
                  

                  Console.WriteLine("KPMG outlook inbox manager");
                  Console.WriteLine("  In progress");
                  
                  Console.WriteLine("    Loading outlook folders");
                  inistialiseFolders();
                  
                  //MailItem oMsg = (MailItem)oItems.GetFirst();
                  int iCount = 0;
                  int iNewCount = 1;
                  
                  Console.WriteLine("    Processing mail items");

                  while (iCount != iNewCount)
                  {
                  
                        Items oItems = oFolder.Items;
                        iCount = oItems.Count;
                        for (int i=1; i<=iCount; i++)
                        {
                              try
                              {
                                    MailItem oMsg = (MailItem)oItems[i];

                                    Console.WriteLine("      Mail item: " + oMsg.Subject);
                                    if (!oMsg.UnRead && hasBeenActioned(oMsg.Subject))
                                    {
                                          oMsg.Move(oReadDestinationFolder);
                                          Console.WriteLine("         moved to junk folder");
                                    }

                                    if (!oMsg.Sent && hasBeenSent())
                                    {
                                          oMsg.Move(oDeletedFolder);
                                          Console.WriteLine("         Deleted");
                                    }


                                    if (!oMsg.UnRead)
                                    {
                                          oMsg.Move(oReadNotActionedDestinationFolder);
                                          Console.WriteLine("         task created");
                                    }
                                    else
                                    {
                                          Console.WriteLine("         not touched");
                                    }
                                                
                              }
                              catch (System.InvalidCastException ice)
                              {
                                    Console.WriteLine("      Not an item of mail");
                              }
                              catch {}
                        }
                        iNewCount = oItems.Count;
                  }      
                  Console.WriteLine("Completed");
                  Console.WriteLine("Press enter to exit application");
                  Console.Read();
            }

            private static bool hasBeenActioned (string mailSubject)
            {
                  bool retval = false;
                  for (int i=1; i<=oSentFolder.Items.Count && retval==false; i++)
                  {
                        try
                        {
                              MailItem msg = (MailItem)oSentFolder.Items[i];
                              if (msg.Subject == mailSubject)
                              {
                                    retval = true;
                              }
                        }
                        catch{}
                  }
                  return retval;
            }


            
            private static bool hasBeenSent()
            {
                  bool retval = false;
                  foreach (MailItem msg in oDeletedFolder.Items)
                        if (msg.Sent)
                        {
                              retval = true;
                              break;
                        }

                  return retval;
            }

            private static ArrayList GetEmailsWithAttachments(MAPIFolder folder)
            {
                  ArrayList list = new ArrayList();
                  foreach (MailItem msg in folder.Items)
                        if (msg.Attachments.Count > 0)
                              list.Add(msg);

                  return list;
            }

            private static void inistialiseFolders()
            {
                  oApp = new ApplicationClass();
                  oNS = oApp.GetNamespace("MAPI");
                  
                  //      oUser = oNS.CreateRecipient("Escott, Robert");
                  oUser = oNS.CreateRecipient("Azunma, Chi");
                  
                  oFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderInbox);
                  oReadNotActionedDestinationFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderTasks);
                  oReadDestinationFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderJunk);
                  oSentFolder = oNS.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
                  oDeletedFolder = oNS.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);

            }



      }
}
There is the MailItem.ReplyRecipients.Count > 0 option, but I am not sure if that will work.

Bob
will try that.
at this stage, i will try anything!!
but  did the code work for you?
MailItem.ReplyRecipients.Count > 0

did not work, i will try something else
I stuxk break pionts here

  if (!oMsg.Sent && hasBeenSent())
                              {
                                   oMsg.Move(oDeletedFolder);
                                   Console.WriteLine("         Deleted");
                              }
and its not even touching the code
I am not quite sure what you are trying to accomplish here, but I have noticed that in the 'hasBeenSent' function, you are looping through the Deleted Items folder.  Is that what you want?

Bob
opps, sorry about this!!
i fink we used the wrong thing here
"oMsg.Sent"

if msg has been read and no attachments then move bin (Deleted items folder)
            if (!oMsg.Sent && hasBeenSent())
      {
            oMsg.Move(oDeletedFolder);
            Console.WriteLine("         Moved to Deleted Items");
      }
what I would like is to move every message which has been read and has no attachments to the (Deleted items folder)
It's this line in 'hasBeenSent':

               foreach (MailItem msg in oDeletedFolder.Items)

That's the Deleted Items folder.  Should that be the Inbox folder instead?

Bob
That's the Deleted Items folder.  Should that be the Inbox folder instead?


yes it should,

so whatever we have there which has been replied and no attachments will be binned

so of each msg in the sent folder which has been replied to and has no attachements wh can been. thats what am
trying to accomplish.

so changed   foreach (MailItem msg in oDeletedFolder.Items)
to

foreach
(MailItem msg in oSentFolder.Items)

      private static bool hasBeenSent()
            {
                  bool retval = false;
                  foreach (MailItem msg in oSentFolder.Items)
                        if (msg.Sent)
                        {
                              retval = true;
                              break;
                        }

                  return retval;
            }
still there
Yes, I am, but I can't find any way of determining whether a MailItem has been replied to or not, and I am doing research, along with the rest of the tasks that I have to accomplish for myself.

Bob
okay, thanks. Am trying other things as well
probably has a sent method
The only way that you can tell if a message has been replied to is to examine the PR_LAST_VERB_EXECUTED property in CDO, since this property is not exposed through the Outlook Automation model, as far as I can tell.  This property doesn't keep track of each verb--it is the last action performed.

Bob
hi,

i fink ConservationIndex will do the trick.
if its a replied msg the ConservationIndex will be greater than the original  sent item.



i used this.
and i inserted a break point to find the values of ConservationIndex

using System;
using Microsoft.Office.Interop.Outlook;
using System.Collections;


namespace EmailSorter
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Loader
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            
            
            
            private static Application oApp;
            private static NameSpace oNS;
            private static Recipient oUser;
                  
            private static MAPIFolder oFolder;
            private static MAPIFolder oReadNotActionedDestinationFolder;
            private static MAPIFolder oReadDestinationFolder;
            private static MAPIFolder oSentFolder;
            private static MAPIFolder oSendToBinFolder;

                        
            
            [STAThread]
            static void Main(string[] args)
            {
                  

                  Console.WriteLine("KPMG outlook inbox manager");
                  Console.WriteLine("  In progress");
                  
                  Console.WriteLine("    Loading outlook folders");
                  inistialiseFolders();
                  
                  //MailItem oMsg = (MailItem)oItems.GetFirst();
                  int iCount = 0;
                  int iNewCount = 1;
                  
                  Console.WriteLine("    Processing mail items");

                  while (iCount != iNewCount)
                  {
                  
                        Items oItems = oFolder.Items;
                        iCount = oItems.Count;
                        for (int i=1; i<=iCount; i++)
                        {
                              try
                              {
                                    MailItem oMsg = (MailItem)oItems[i];
                                  string test = oMsg.CC;
                                    string index = oMsg.ConversationIndex;
                                    string topic =oMsg.ConversationTopic;
                                    Console.WriteLine("      Mail item: " + oMsg.Subject);

                                    if (!oMsg.UnRead && hasBeenActioned(oMsg.Subject))
                                    {
                                          oMsg.Move(oReadDestinationFolder);
                                          Console.WriteLine("         moved to junk folder");
                                    }

                                    if (!oMsg.Sent && hasBeenSent())
                                    {
                                          oMsg.Move(oSendToBinFolder);
                                          Console.WriteLine("         Moved to Deleted Items");
                                    }


//                                    if (!oMsg.UnRead && hasBeenSent(oMsg.Subject))
//                                    {
//                                          oMsg.Move(oSendToBinFolder );
//                                          Console.WriteLine("         Moved to Deleted Items");
//                                    }
//

                                    if (!oMsg.UnRead)
                                    {
                                          oMsg.Move(oReadNotActionedDestinationFolder);
                                          Console.WriteLine("         task created");
                                    }
                                    else
                                    {
                                          Console.WriteLine("         not touched");
                                    }
                                                
                              }
                              catch (System.InvalidCastException ice)
                              {
                                    Console.WriteLine("      Not an item of mail");
                              }
                              catch {}
                        }
                        iNewCount = oItems.Count;
                  }      
                  Console.WriteLine("Completed");
                  Console.WriteLine("Press enter to exit application");
                  Console.Read();
            }

            private static bool hasBeenActioned (string mailSubject)
            {
                  bool retval = false;
                  for (int i=1; i<=oSentFolder.Items.Count && retval==false; i++)
                  {
                        try
                        {
                              MailItem msg = (MailItem)oSentFolder.Items[i];
                              if (msg.Subject == mailSubject)
                              {                  
                                    retval = true;
                                    
                              }
                        }
                        catch{}
                  }
                  return retval;
            }


            
//            private static bool hasBeenSent (string MailSent)
//            {
//                  bool retval = false;
//                  foreach (MailItem msg in oSendToBinFolder.Items)
//                        if (msg.Subject == MailSent)
//                        {
//                              retval = true;
//                              break;
//                        }
//
//                  return retval;
//            }
//
            private static bool hasBeenSent()
            {
                  bool retval = false;
                  foreach (MailItem msg in oSentFolder.Items)
                        if (msg.Sent)
                        {
                              retval = true;
                              break;
                        }

                  return retval;
            }


            private static ArrayList GetEmailsWithAttachments(MAPIFolder folder)
            {
                  ArrayList list = new ArrayList();
                  foreach (MailItem msg in folder.Items)
                        if (msg.Attachments.Count > 0)
                              list.Add(msg);
                  

                  return list;
            }

            private static void inistialiseFolders()
            {
                  oApp = new ApplicationClass();
                  oNS = oApp.GetNamespace("MAPI");
                  
                  //      oUser = oNS.CreateRecipient("Escott, Robert");
                  oUser = oNS.CreateRecipient("Azunma, Chi");
                  
                  oFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderInbox);
                  oReadNotActionedDestinationFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderTasks);
                  oReadDestinationFolder = oNS.GetSharedDefaultFolder(oUser, OlDefaultFolders.olFolderJunk);
      
                  oSentFolder = oNS.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
                  oSendToBinFolder = oNS.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
                         
            }



      }
}
And did it work?  Does it work for messages that we forwarded, but not replied to?

Bob
it has worked yet.

i need to write one of these
 private static bool hasBeenSent()
          {
               bool retval = false;
               foreach (MailItem msg in oSentFolder.Items)
                    if (msg.Sent)
                    {
                         retval = true;
                         break;
                    }

               return retval;
          }

to evaluate if a msg is replied or forwarded
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks bob, will try this code on monday.
hi bob, am more focused now i shall close this and will ask the question again.

thanks