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:
Administrator receives mail from John:
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:
John receives email that appears to be written by Jane:
Okay that was our scenario and now let’s see how it was created.
Step 1: Well this is pretty obviously :-)
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.):
public partial class ThisAddIn
MailItem _mailItem = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
this.Application.ItemLoad += new
void Application_ItemLoad(object Item)
if (Item is MailItem)
MailItem mailItem = Item as MailItem;
mailItem.Open += new
_mailItem = mailItem;
catch (System.Exception ex)
void mailItem_Open(ref bool Cancel)
if (_mailItem.Subject.Contains("Jane") == true)
// This requires "Send As" privileges at the AD:
_mailItem.SentOnBehalfOfName = "email@example.com";
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
#region VSTO generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InternalStartup()
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
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:
Publishing wizard asks the location for the published files:
After that published location contains following files:
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):
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:
And when prereqs are ok we’re ready to install our app (this screenshot is from Windows Server 2003):
After the installation add-in can be seen in Add Remove Programs (and removed if needed):
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):
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:
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