Simple Outlook Add-in with VSTO 3.0

Creating add-ins with Visual Studio 2008 and VSTO 3.0 (Visual Studio Tools for Office) is easy and fast… Just use File –> New –> Project –> Select correct add-in i.e. Outlook 2007 Add-in and you project template is created. And you can run you add-in in debug by just hitting F5! So the basic setup is just few clicks and then you’re ready to start implementing your actual functionality i.e. extending ribbon etc.

In this post I’m going to create simple add-in that demonstrates how you can send emails on behalf of someone else and the selection for doing that is done automatically. Let’s go through the scenario since it’s easier to understand the functionality that way.

I have users John, Jane and Administrator. John wants to contact Jane but doesn’t know how to contact her so he sends out email to the administrator:

 EmailToAdmin

Administrator receives mail from John:

EmailFromJohn

Since Administrator knows Jane’s home phone he can contact her in urgent matters and get her message back to John. In this case Administrator just presses Reply to John’s email and writes the message as Jane dictates it over the phone:

EmailResponseToJohn

John receives email that appears to be written by Jane:

EmailResponseAtJohnsOutlook

 

Okay that was our scenario and now let’s see how it was created.

Step 1: Well this is pretty obviously :-)

Addin1

Step 2: Write following code (and as always.. use this at your own risk :-). This should be extended with better error handling, sanity checks etc.):

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
 using Microsoft.Office.Interop.Outlook;using System.Windows.Forms;namespace My_Outlook_2007_Add_in{  public partial class ThisAddIn  {    MailItem _mailItem = null;    private void ThisAddIn_Startup(object sender, System.EventArgs e)    {      this.Application.ItemLoad += new        ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad);    }    void Application_ItemLoad(object Item)    {      if (Item is MailItem)      {        try        {          MailItem mailItem = Item as MailItem;          mailItem.Open += new             ItemEvents_10_OpenEventHandler(mailItem_Open);          _mailItem = mailItem;        }        catch (System.Exception ex)        {          MessageBox.Show(ex.ToString(),             "Exception",             MessageBoxButtons.OK, MessageBoxIcon.Error);        }      }    }    void mailItem_Open(ref bool Cancel)    {      if (_mailItem.Subject.Contains("Jane") == true)      {        // This requires "Send As" privileges at the AD:        _mailItem.SentOnBehalfOfName = "jane@contoso.com";      }    }    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)    {    }    #region VSTO generated code    /// <summary>    /// Required method for Designer support - do not modify    /// the contents of this method with the code editor.    /// </summary>    private void InternalStartup()    {      this.Startup += new System.EventHandler(ThisAddIn_Startup);      this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);    }        #endregion  }}

Code works so that we first trap events from ItemLoad (on line 11). In Application_ItemLoad we check that is this current item MailItem and if it is we then hook Open event of that Item. And when mail item is opened we’ll check that does that contain Jane at the subject. And if it does then we set the SentOnBehalfOfName property for the mail item. You need to modify contents between lines 37 and 41 to make this work in real life. I just made this as simple as possible. So probably adding few more checks isn’t bad idea :-)

Step 3: Build, Test and when everything is working then Publish:

Publish0

Publishing wizard asks the location for the published files:

Publish1

Publish2

Publish3

After that published location contains following files:

Folder1

Setup.exe installs the add-in. Application Files folder contains subfolder for each version of the add-in (since it will be published to same location on update). You can actually control the check for new versions from VS (by default add-in checks for update once per week):

VS

Now you can install the add-in for all the necassary users. Setup also knows about prerequisites (.NET framework 3.5 and VSTO 3.0 Runtime) and knows how to get them during install:

PreReq1

PreReq2

And when prereqs are ok we’re ready to install our app (this screenshot is from Windows Server 2003):

 Folder2

After the installation add-in can be seen in Add Remove Programs (and removed if needed):

AddRemove

Now your Outlook will work as described at the beginning of the post.

 

Hmm… you might start wondering that isn’t there few security issues in this approach? How can administrator act as Jane and how that isn’t displayed at his Outlook? Well by default security forbids this kind of scenario but if this kind of approach is needed then you need to give permission to the users at the AD (Important note: I gave Everyone permissions to “Send As” but that is not the way to go in real life):

AD

And what does Outlook do then? In previous screenshots Outlook didn’t notify administrator about the SentOnBehalfOfName property change. However if you add more email addresses to your email then Outlook refreshes the view to this:

Outlook

So Outlook added From… field and it now clearly indicates that this message will be send as Jane. Outlook just makes sure that user is aware of the senders change. Obviously our add-in could inform the end user with some kind of tooltip or something like that but I’ll leave those details to you.

Well here was small example of Outlook add-in. I’ll probably come back to add-ins in near future so stay tuned…

Anyways… Happy hacking!

J