Altijd als eerste onze nieuwste blogs lezen? Laat je email adres achter en je ontvangt een bericht als wij een nieuw blog plaatsen.

How great would it be when you get a notification from your computer if somebody treats to pie!? Very great, indeed! With a simple plugin for Microsoft Outlook this is realizable, so here is how one can make such thing!

Pie alarm

Onze technische blogs zijn in het Engels. Dit doen wij omdat wij menen dat technische kennis van Sitecore en Sitefinity grensoverschrijdend moet zijn. Wij leren veel van buitenlandse developers, wij delen onze kennis ook graag met hen. Mochten er in de code fouten zitten of punten ter verbetering, dan gaan we graag het gesprek aan.  

Many things that are being programmed, are programmed to make life easier for us. With a textwriter, for example, we can easier type some text than using a pen and with our phone we can easier talk to somebody far away than travelling to that person. And just like that, being notified about somebody treating to pie has its benefits. Think about being the first one there to pick the largest and most delicious piece, before the alpha male comes over and claims it.

And despite such great benefits, the pie alarm is an easy piece of code. It is a plugin for outlook and an easy example of how to work with Microsoft Visual Studio Tools for Office (VSTO). To be able to write plugins for Microsoft Office programs, you will need to install the Developer Tools for Office. With these tools installed, you have the option to create an Outlook VSTO Add-in:

This will create a new project with a class called ‘ThisAddIn.cs’.

In this class we can bind new actions to certain events in Outlook. In this case we want to check the contents of incoming emails. This can be done by attaching a new event handler to the incoming mail event. So we need to create this event handler and get the email contents by using the string parameter of the handler as follows:

void Application_NewMailEx(string anEntryID)

{

    Outlook.NameSpace outlooknamespace = this.Application.GetNamespace("MAPI");

    Outlook.MAPIFolder folder = outlooknamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

    Outlook.MailItem mailItem = (Outlook.MailItem)outlooknamespace.GetItemFromID(anEntryID, folder.StoreID);

 

    string subject = mailItem.Subject.ToUpper();

    string body = mailItem.Body.ToUpper();

 

    //check if the subject or body contains "pie", for example: if(subject.StartsWith("PIE ") || subject.Contains(" PIE ")) { Console.Beep(2000, 2000); }

}

When the contents of the incoming mail are retrieved, we can do anything with it such as sounding a beep depending on certain conditions (written in pseudo code above). Now that we have a function that can handle the incoming email, we only need to attach this function to the incoming mail event. We can do this in the startup event handler, which is already present in the ThisAddIn class.

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

    this.Application.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx);

}

If we now build and publish the solution and run the installer which will be generated by the publish, Outlook will trigger on incoming emails which contain the word “pie”!

As showed, it is very easy to make a plugin for a Microsoft Office product like Outlook. Writing a plugin for Word, Excel and other applications can be done the same way. Of course you can make much more complex plugins if that is necessary to fill your own or your customers needs!

Meer achtergrond nodig? Neem contact met ons op

Deel deze pagina