Search code examples
.netvb.netvstooutlook-addindotnet-httpclient

How to make a REST call when specific events occur in Outlook 365 Using a VSTO Add-In


I am trying to make an Outlook 365 add-in that does the following:

When an email is flagged for follow up (Today, Tomorrow, This Week, etc):

  • A rest API call is made to a server, eg: http://localhost:8080/email/flagged, with post data that includes the email from, subject, body, and flag name (today, tomorrow, etc). If each email has a unique id of some sort, sending that value as well.

When an email is UN-flagged for follow up:

  • A rest API call is made to a server, eg: http://localhost:8080/email/unflagged/{uniqueEmailId} , if there is a unique id assigned to each email available for use, otherwise post data that contains the from and subject values from the email.

I am looking at using VSTO but I have never used it or any other of the MS Office add-in tools.

I have too many questions, for starters:

  1. Can an outlook add-in make HTTP REST calls to an outside web API service?
  2. Are there exposed events in the ThisAddIn object related to when an email is flagged/unflagged?

As far as code, I have a blank slate (VB shown but will accept C# suggestions too):

Dim WithEvents currentExplorer As Outlook.Explorer = Nothing

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles Me.Startup

     'register a handler for the events I am interested in (need help here)

End Sub

Private Sub MyEventHandler(someEventObject)
   'Get basic data (need help here too), eg:
   Dim eventName = someEventObject.EventType
   Dim EmailUniqueId = someEventObject.Uid
   Dim EmailFrom = someEventObject.FromEmailAddress
   Dim Subject =  someEventObject.Subject

   'Send an HTTP REST call (I know how to do that) 
End Sub

FYI - I am a web developer so I naturally want to gravitate to a solution that would send an HTTP REST API call for this. However, if that is not possible I am open to other ideas for getting these event details out of Outlook, even going so low as to create a file that contains the basic event/email information in a folder that could be parsed by a scheduled job. This project is just something I am making for myself to use for work so it doesn't have to be elegant.


Solution

  • Of course, anything a regular Windows app can do, your VSTO addin can do just as easily.

    To make an HTTP request, take a look at the HttpClient class

    To monitor events on a particular folder use Items.ItemAdd/ItemChange events. The item the that caused the event will be passed to your event handler. Since you can have different types of items in the same folder (e.g. ReportItem and MailItem objects), the parameter is declared as a generic object and you will need to explicitly interrogate the object type using the as or is operators.

    To open the folder you are interested in, you can use, for example, Globals.ThisAddin.Application.Session.GetDefaultFolder(olFolderInbox).