I have spent a few days looking into this and it seems that it's very difficult to add custom code to an incoming email event. And many of the methods of doing it will just get ignored if Exchange thinks it's slowing down the email system. (http://www.outlookcode.com/article.aspx?id=62)
The other method is to hook into the SMTP events which works but feels at bit of a hack. To do that you need to write a wscript then register that against the event of arrival of an email.
Here is an example vb script for adding a random hex reference onto every email which comes via SMTP.
<SCRIPT LANGUAGE="VBScript">
Sub IEventIsCacheable_IsCacheable()
'To implement the interface, and return S_OK implicitly
End Sub
Sub ISMTPOnArrival_OnArrival(ByVal Msg, EventStatus)
Dim Flds
randomize()
set Flds = Msg.Fields
With Flds
strSubject = .Item("urn:schemas:httpmail:subject")
if instr(strSubject, "Ref=") = 0 then
strSubject = Replace(strSubject, CHR(9), "") & " (Ref=" & hex(replace(timer(), ".", "")) & hex(rnd()) & ")"
.Item("urn:schemas:httpmail:subject") = strSubject
.Update
else
strSubject = LEFT(strSubject, instr(strSubject, "(Ref=") - 1) & MID(strSubject, instr(instr(strsubject, "(Ref="), strSubject, ")") + 1, Len(strSubject)) & " (Ref=" & hex(replace(timer(), ".", "")) & hex(rnd()) & ")"
.Item("urn:schemas:httpmail:subject") = strSubject
.Update
end if
End With
Msg.Datasource.Save
EventStatus = 0 'Run next sink
End Sub
</SCRIPT>
Then to register the script run this.
Cscript smtpreg.vbs /add 1 onarrival SMTPAddRef CDO.SS_SMTPOnArrivalSink "mail from=*"
Cscript smtpreg.vbs /setprop 1 onarrival SMTPAddRef Sink ScriptName "C:\ENTERPATH\SMTPRef.vbs"
To unregister the script run the follow;
cscript smtpreg.vbs /remove 1 onarrival SMTPAddRef
The most resilient method seems to be to create a timer based system to check for new mails every X minutes.
Not as slick as I was hoping for but it'll do.
Hope this helps others.