Archive for October, 2009

Creating an Outlook add-in to write new messages to disk

No Comments »

In this post I will describe how to create an add-in for Outlook which will cause Outlook to write new messages to a folder on disk.

I’ve created the add-in using Visual Studio 2010, since I couldn’t get the Visual Studio Tools for Office (VSTO) to work for my VS 2008 installation.

After firing up VS 2010 choose Visual C# –> Office –> 2007 –> Outlook 2007 Add-in:

image

Visual Studio will create a class called ThisAddIn. You will initially only see a partial class in the file ThisAddIn.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace NewItemsPersister
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        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
    }
}

Visual Studio will also have created a file called ThisAddIn.Designer.cs which contains the bulk of the class. You will not need to bother with this file, but it is nice to know that it is there.

Obviously, the ThisAddIn_Startup method will execute when the add-in is loaded while the ThisAddIn_Shutdown method will execute when the add-in is unloaded.

To execute code when Outlook downloads a new message, we will need to register an appropriate event handler. The ThisAddIn class has an Application property (auto-generated by the Visual Studio template) which you can use to access an event firing whenever a new mail is received.

Thus, we update the class to contain the following code:

public partial class ThisAddIn
{
    private ApplicationEvents_11_NewMailExEventHandler handler;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.NewMailEx += handler;
    }

    void Application_NewMailEx(string EntryIDCollection){ }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        this.Application.NewMailEx -= handler;
    }

    #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);

        handler = new ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx);
    }

    #endregion
}

The ApplicationEvents_11_NewMailExEventHandler delegate (found in the Microsoft.Office.Interop.Outlook namespace) will be passed a string containing a comma-separated list of identifiers for the received mails. Once you have such an identifier, you can get a hold of the corresponding mail like this:

MailItem item = (MailItem)Application.Session.GetItemFromID(entryId, null);

To save the MailItem, use the MailItem’s SaveAs method. This method takes two parameters: the path in which to save the message and an instance of the OlSaveAsType enumeration designating the file type.

To have the add-in save new files to disk in the C:\tmp folder, update the Application_NewMailEx method as follows:

void Application_NewMailEx(string EntryIDCollection)
{
    string[] entryIdArray = EntryIDCollection.Split(',');
    foreach (string entryId in entryIdArray)
    {
        try
        {
            MailItem item = (MailItem)Application.Session.GetItemFromID(entryId, null);
            string fullpath = string.Format(@"c:\tmp\{0}.msg", item.Subject);
            item.SaveAs(fullpath, OlSaveAsType.olMSG);
        }
        catch (System.Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
}

When you build the application four files will be generated:

image

Double-clicking the NewItemsPersister.vsto will install the add-in.

Now, whenever I receive a mail, the mail will immediately be written to the disk:

image

image

When developing Outlook add-ins, you will often want to uninstall the add-in to be able to install an updated version. To uninstall the add-in, use Control Panel –> Programs and Features.

NOTE: when developing the add-in above, I continually ran into trouble when reinstalling the add-in. I would get an error saying that the add-in could not be installed, because it was already installed. This happened even though I had removed the add-in using Control Panel –> Programs and Features. To solve this, I had to clear the ClickOnce install cache using the Visual Studio command prompt:

image

More on the Manifest Generation and Editing Tool (Mage.exe) here.


Presenting at the Microsoft Web Partner event Oct. 7th

No Comments »

I will be giving a high-level presentation on the ASP.NET MVC framework at Microsoft’s Web Partner event on October 7th.

Participation is free for web partners. Register here.

Update: The slides for the presentation are now available here.