Monday, June 14, 2010

Useful Outlook Plug-in

If you are anything like me, you will have probably sent mails referring to an attachment without actually including the attachment in this mail.

So, anyone feel like this has happened to you, well here is some small tool that could prove useful.

To build it just create Outlook 2007 plug-in in Visual Studio, and add the following code.

public partial class ThisAddIn

   private Outlook.MailItem MailItem;

   private void ThisAddIn_Startup(object sender, System.EventArgs e)
   {
      this.Application.Inspectors.NewInspector +=   
      Inspectors_NewInspector;
   }

   void Inspectors_NewInspector(Inspector inspector)
   {

     Outlook.MailItem mailItem = inspector.CurrentItem 
     as Outlook.
MailItem;

     if (mailItem != null)
     {
       MailItem = mailItem;

       if (MailItem.EntryID == null)
       {
          ((
ItemEvents_10_Event)MailItem).Send += SendMail;
       } 
     }
   }

   private void SendMail(ref bool cancel)
   {

      if (MailItem.Attachments.Count == 0  && (MailItem.Body.ToLower()
          .Contains(
"attachment"))
      {
     
   if (MessageBox.Show("Are you sure you want to send this mail 
             without an attachment?"
, "Are you sure ?"
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
              DialogResult.No)
             {
                cancel =
true;
             }
      }
   }  

   private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
   {
     ((ItemEvents_10_Event)MailItem).Send -= SendMail;
     this.Application.Inspectors.NewInspector –= Inspectors_NewInspector;

   }
}

 

So what exactly are we doing here ?

Basically it’s simply getting an inspector for a MailItem, and then intercepting the Send event.

A send event is triggered whenever a user clicks on send while creating an email.
Our code will simply check if this user has mentioned an attachment in his email and ask for confirmation to send this mail without any attachments.