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.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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

Comments (7)

  1. Dawie van Niekerk says:

    Hi Janne,

    I see your using click-once and I know it is the prefered method, but I need to deploy my addin inside another MSI.

    So the question is do you have a procedure to create an MSI for a VSTO 3.0 Outlook addin created in Visual Studio 2008?

    I know it is not as simple as adding a deployment project to the solution. There is probably registry issues that has to be resolved.

    Thanks,

    Dawie

  2. Jonas Van Gestel says:

    Hi Janne,

    Thanks for sharing this.

    I made an Add-In (based on your code) to change the Reply To address to a distribution group.

    Thanks,

    Jonas

  3. G Patel says:

    Hi Janne,

    It gave me good start.

    Thanks,

  4. Sujay says:

    Hi Jane,

    Thanks for the info. I would like to know if there is a designer for designing my addin

    Thanks

    Sujay

  5. Anand says:

    Hi,

    Tanks for your useful post. and please provide me url where i can download VSTO for VS2008

  6. Nicodemus Naamwinkpeng Deanata says:

    Please I am interested

  7. Hussain Badusha says:

    Hi,

    Thanks for the post.
    I’ve installed outlook 2016 and visual studio 2015 in my machine.
    I followed the above procedures to an add-in but it is not working as expected.
    What’d be the problem?

    Any help would be greatly appreciated.

    Regards,
    Hussain Badusha.

Skip to main content