Partager via


Outlook 2010 Plain Text Folder …

Download Source

A customer asked me recently “I would like to see a plain text version of selected emails without sending them to the junk folder.”  I looked for a rule, but I couldn’t find one.  I looked for a setting, I couldn’t find one.  The only other way I could think of doing it is with an Outlook Add-in.

By the way, if you have a better way of doing this, please let me know!  Other than that, here’s the Add-in.  (If you seek more information about building Add-ins with Visual Studio 2010, check out the VSTO Developer Center.)

 private void ThisAddIn_Startup(object sender, System.EventArgs e)
 {
     Outlook.MAPIFolder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
     Outlook.MAPIFolder sandbox = null;
  
     try
     {
         sandbox = GetSandbox(inbox, "Sandbox");
     }
     catch (Exception)
     {
         inbox = null;
         return;
     }
  
     if (sandbox == null)
     {
         return;
     }
  
     sandbox.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(SandboxFolderItemAdded);
 }
 private Outlook.MAPIFolder GetSandbox( Outlook.MAPIFolder rootFolder, string sandboxName )
 {
     Outlook.MAPIFolder retVal = null;
     
  
     if (String.IsNullOrEmpty(sandboxName) || rootFolder == null)
     {
         return retVal;
     }
  
     foreach (Outlook.MAPIFolder folder in rootFolder.Folders)
     {
         if (folder.Name.Equals(sandboxName, StringComparison.CurrentCultureIgnoreCase))
         {
             retVal = folder;
             break;
         }
     }
  
     return retVal;
 }
  
 private void SandboxFolderItemAdded(object addedItem)
 {
     Outlook.MailItem mi = addedItem as Outlook.MailItem;
  
     if (mi == null)
     {
         return;
     }
  
     mi.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatPlain;
     mi.Save();
 }

The Add-in will start at the Inbox and look for a folder beneath the Inbox called “Sandbox”.  This behavior is easy enough to change in the ThisAddIn_Startup  method.  (In case, for example, you want to traverse a deeper tree inbox->something->something->Sandbox.)  It will then hook the ItemAdd method and use it to coerce the type of any messages added to the folder into Plain Text.  If you just want to use the solution for yourself, you can build and deploy it from VS.  If you want to deploy it more widely, here’s a good article on how that can be accomplished.

A few words of warning.  First there are a lot of vexing exceptions when dealing with Office Add-ins.  (I guess it just the nature of the Interop).  For example, if you index into a Folders collection with a key that is not present, you get an exception instead of just a null value.  Also, unhandled exceptions in Add-ins can stop Outlook dead in its tracks without so much as a warning to the user that something has gone wrong.  Dealing with unhandled exceptions in Office Add-ins is beyond the scope of my patience for work-arounds.  More information on that subject if you dare.

By the way, this is exactly how I build my Add-in that coerces all my response to HTML regardless of what format the original email was sent in.  That’s right – to heck with the sender, I want my signatures to work.  (Also, I figure if your client does not handle HTML at this point, you deserve the shame and ridicule.) You can imagine hooking the inbox.Items.ItemAdd event with this code …

 private void InboxFolderItemAdded( object addedItem )
 {
   Outlook.MailItem mi = addedItem as Outlook.MailItem;
  
   if( mi == null )
   {
     return;
   }
  
   if( mi.BodyFormat != Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML )
   {
     mi.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
     mi.Save();
   }
 }

And you would have all your emails in HTML format.