Friday, January 14, 2005

Tracking DL-usage with a public folder Event sink

This week I’ve been thinking about issues of trying to track DL usage in a large networks.The idea of tracking distribution list usage by adding a public folder to the DL has been around for a while and offers a good solution. Some problems that do arise from this method is how many folders do you create (eg one per DL) or do you dump then all into one collection spot. The other problem is if someone is BCC’ing the DL its very hard to determine which DL that a message that you tracked was sent to. Back in Exchange 5.5 you used to have a unique event logged (by the MTA which used to perform DL expansion) in the message tracking logs that you could use to work out DL usage. Now in Exchange 2 x DL expansion is handled by the categorizer which has its own tracking log event ID’s but the problem is there’s no difference between a Categorizer event logged for a message and one logged for a DL. The other challenge if you are going to use the Categorizer event on Exchange 2x is that for a categorizer event for that message with DL as a recipient is only going to be logged possibly on two servers. Eg you will have a Categorizer event on the server where the message was originally sent (you’ll have categorizer events for all the unique(non DL) recipients on this server) and you’ll have a categorizer event for the DL on the DL’s expansion server (if a specific expansion server is set otherwise it may only be on the source server if that’s where the DL gets expanded)

So what I decided to do is see if I could create a sink that would look at messages that arrived in my public DL tracking folder. Look at the source where that mail originated and find the home server of the user that sent it. Then connect to the Message Tracking logs on that server and look for all the categorizer “Event 1024” for that particular messageID. Then lookup the recipients in Active Directory to see if they are groups or Query Based DL’s. Then grab the mail address and displayname of all the DL’s that this message was sent to and then update the too address of the email so it contained the names of the DL it was sent to. So no matter even if a DL was only BCC ‘d the Sink would always put that address in the too field so you could tell who it was sent to.

The sink itself uses a combination of CDOEX to open the message grab the messageID, ADSI to find the sending users home server. Then WMI to find the message in the tracking log then ADSI again to search for the recipients in Active Directory to see if they where DL’s and finally CDOEX to update the email. The only real problem I had is that I had to delegate my event sink users view only administration access to my Exchange environments so it had the rights to search the message tracking logs. I’m still thinking about whether this was a good idea or not.

I’ve post a downloadable copy of the sink up here
The code looks like


Sub ExStoreEvents_OnSave(pEventInfo, bstrURLItem, lFlags)
Const EVT_NEW_ITEM = 1

If (lFlags And EVT_IS_DELIVERED) Or (lFlags And EVT_NEW_ITEM) Then

set msgobj1 = createobject("CDO.Message") bstrURLItem,,3
strComputerName = search_user(replace(replace(msgobj1.fields("urn:schemas:httpmail:fromemail"),">",""),"<",""),2)
if len(strComputerName) <> 0 then
strmessid = replace(replace(msgobj1.fields("urn:schemas:mailheader:message-id"),">",""),"<","")
'Message ID to search for
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_MessageTrackingEntry"
objdlladdress = ""
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" & strComputerName &
"/" & cWMINameSpace
Set objWMIExchange = GetObject(strWinMgmts)
Set listExchange_MessageTrackingEntries = objWMIExchange.ExecQuery("Select *
FROM Exchange_MessageTrackingEntry where entrytype = '1024' and MessageID = '" _
& strmessid & "'",,48)
For each objExchange_MessageTrackingEntry in listExchange_MessageTrackingEntries
for i = 0 to objExchange_MessageTrackingEntry.RecipientCount
objtempaddr =
if len(objtempaddr) <> 0 then
if len(objdlladdress) <> 0 then
objdlladdress = objdlladdress & ";" & objtempaddr
objdlladdress = objtempaddr
end if
end if
objtempaddr = ""
if len(objdlladdress) <> 0 then msgobj1.fields("urn:schemas:mailheader:to") =
msgobj1.fields("") =
set objWMIExchange = nothing
set listExchange_MessageTrackingEntries = nothing
end if
set msgobj1 = nothing
end if
end sub

function search_user(senaddr,searchtype)
Set objDNS = CreateObject("ADSystemInfo")
DomainName = LCase(objDNS.DomainDNSName)
Set oRoot = GetObject("LDAP://" & DomainName & "/rootDSE")
strDefaultNamingContext = oRoot.get("defaultNamingContext")
if instr(senaddr,"/CN=") then
strQuery = "select distinguishedName,mail,displayname,objectCategory from
'LDAP://" & strDefaultNamingContext & "' where legacyExchangeDN = '" & senaddr &
strQuery = "select distinguishedName,mail,displayname,objectCategory from
'LDAP://" & strDefaultNamingContext & "' where mail = '" & senaddr & "'"
end if

Set oConn = CreateObject("ADODB.Connection") 'Create an ADO Connection
oConn.Provider = "ADsDSOOBJECT" ' ADSI OLE-DB provider
oConn.Open "ADs Provider"

Set oComm = CreateObject("ADODB.Command") ' Create an ADO Command
oComm.ActiveConnection = oConn
oComm.Properties("Page Size") = 1000
oComm.CommandText = strQuery
Set rs = oComm.Execute
while not rs.eof
if searchtype = 1 then
if instr(lcase(rs.fields("objectCategory")),"cn=group,") or
dispname = rs.fields("displayname")
emailad = rs.fields("mail")
search_user = chr(34) & dispname & chr(34) & "<" & emailad & ">"
end if
strsendingusername = "LDAP://" & rs.Fields("distinguishedName")
set objsenduser = getobject(strsendingusername)
inplinearray = Split(objsenduser.msExchHomeServerName, "=", -1, 1)
strservername = inplinearray(ubound(inplinearray))
search_user = strservername
end if

end function