Adding Custom Headers to Email Messages

There are times when a system administrator may want, or need, to add a custom header to messages that pass through their SmarterMail server. For example, when using a third-party spam service or antispam appliance that requires a specific header to be included in messages for reporting and/or routing purposes. Previously, this had to be handled by SmarterTools and the new header was included in every new release of our products. That's not terribly efficient...
 
Therefore, we created a Visual Studio project that can be used to monitor the SmarterMail proc folder and append a header -- or headers -- to messages. This project also gives you an example of how the service can be used. This project is released, essentially, as an "open source" project so it can be built upon or otherwise modified to do what you need to the messages before they hit your spool. Once compiled and installed, it will run as a Windows service and begin modifying headers.

What Does the Example Do?

This example project covers the basics of adding a custom header to any and all messages that are processed through the /proc folder: it sets up the watcher for file name changes, deals with messages already in the folder, parses the .hdr's of the messages into a dictionary, reads in the email then adds a custom header. The header that's added is named "X-Src-Sender" and it includes 3 additional items: the sender's hostname, IP address and port. The messages are then placed in your spool for processing.
 
As this is just our example, you'll want to modify it as needed to add whatever you want to the header of your messages. 
 
Download the Project from our GitHub repository: SmarterMail Custom Header Script


Editing and Building the Service

First off, you'll want to edit the project files. The easiest way to do this, if you don't already have Microsoft Visual Studio installed, is to get the Community Edition. Visual Studio Community Edition is free and is more than capable of being used to edit, then compile, the project files into a service. If you're unsure how to "build" the project into a service, Microsoft has a great walk though for building a service using Visual Studio that provides step-by-step instructions.