When writing an email add-in for Outlook with Visual Studio, it’s really quite easy to get the email message object while the send event is in progress using the Application.ItemSend event. However this event happens before the email is sent, so although it can be useful, the Application.ItemSend event is often not the correct event handler to be used when working with Outlook email messages programmatically.
In my case, what I want to be able to do is to get and copy an email object when it is added to the sent items folder, but so far this has been difficult to figure out. Until a few minutes ago, that is. After a bit of research and investigation I’ve got it working.
The trick is to register a custom event handler when Outlook is opened using the Me.Startup event on the ThisAddIn class.
Specifically, in the Me.Startup event, assign the Outlook.OlDefaultFolders.olFolderSentMail folder to a member variable and manually set an event handler to the folder consisting of the folder’s Items.ItemAdd trigger and point it to a custom method passing the item as an object argument.
Sound complicated? It’s actually not really. Here’s the code:
Imports System.IO Imports System.Security.Principal Imports System.Windows.Forms Public Class ThisAddIn Dim sentItems As Outlook.Items Dim sentFolder As Outlook.Folder Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup ' Executes When Outlook is opened sentFolder = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) sentItems = sentFolder.Items AddHandler sentItems.ItemAdd, AddressOf itemadd End Sub Sub itemadd(ByVal NewEmailItem As Object) Dim sentMessageItem As Outlook.MailItem = CType(NewEmailItem, Outlook.MailItem) If Not sentMessageItem Is Nothing Then ' Add your code here End If End Sub
Things to Keep in Mind:
As you can see, the above way of accessing newly added email messages in Outlook ‘s sent items folder is straightforward and exactly what one needs when working with Outlook email messages.
This is especially useful since an email message is only assigned a unique Id by Outlook when it is saved or sent. This means if you do processing of the message in the Application.ItemSend email message event, you will not be able to retrieve a unique Id for the email.
Another interesting caveat is that if you want to back up an email message into a separate Outlook folder, you should not do so in the Application.ItemSend event. There are two good reasons for this:
- You will be copying and backing up the unsent email, which can be confusing to users since they will think that the message was not sent.
- The copied and unsent message will be permanently kept in Outlook’s Outbox. This will make it look like the add-in that you have written is breaking Outlook and will cause confusion to users since they will see their Outbox full of unsent messages.
However it is often impossible to completely code for the Sent Items folder Items.ItemAdd event handling method, since for example custom form information is no longer available at that point. Basically one needs a way to interact with an email message across the message’s life-cycle stages. This can be done by splitting the code for handling Outlook messages between the Application.ItemSend event and the custom Sent Items folder Items.ItemAdd event handling method.
Because an email message is only given a unique Id when it is in the sent status, there is no clean way of tracking the email from the Application.ItemSend event to the custom Sent Items folder Items.ItemAdd event handling method.
In this case I found that the cleanest way to correct for this is to generate and store my own unique email Id. Since Outlook allows custom user properties, I add the unique Id as a custom user property in the Application.ItemSend event. Alternately one can add the form information directly as a series of custom user properties.
Then, in the Sent Items folder Items.ItemAdd event I can tell if a message requires special handling by retrieving that Unique Id custom user property. This is especially useful when storing and retrieving Outlook email information from a custom database.
Outlook custom add-in components are a powerful tool that Microsoft has made available with Visual Studio and .NET 4. I intend to continue exploring the features and functionality available to programmers, and would appreciate your feedback or suggestions in the comments section below.