troubleshooting Question

outlook programming in c#

Avatar of SirReadAlot
SirReadAlot asked on
C#
33 Comments1 Solution592 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 33 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 33 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros