Skip to main content

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

<SCRIPT LANGUAGE="VBScript">

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

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

set msgobj1 = createobject("CDO.Message")
msgobj1.datasource.open 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 =
search_user(objExchange_MessageTrackingEntry.RecipientAddress(i),1)
if len(objtempaddr) <> 0 then
if len(objdlladdress) <> 0 then
objdlladdress = objdlladdress & ";" & objtempaddr
else
objdlladdress = objtempaddr
end if
end if
objtempaddr = ""
next
Next
if len(objdlladdress) <> 0 then msgobj1.fields("urn:schemas:mailheader:to") =
objdlladdress
msgobj1.fields("http://schemas.microsoft.com/exchange/outlookmessageclass") =
"IPM.Note"
msgobj1.fields.update
msgobj1.datasource.save
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 &
"'"
else
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
instr(lcase(rs.fields("objectCategory")),"cn=ms-exch-dynamic-distribution-list,")
then
dispname = rs.fields("displayname")
emailad = rs.fields("mail")
search_user = chr(34) & dispname & chr(34) & "<" & emailad & ">"
end if
else
strsendingusername = "LDAP://" & rs.Fields("distinguishedName")
set objsenduser = getobject(strsendingusername)
inplinearray = Split(objsenduser.msExchHomeServerName, "=", -1, 1)
strservername = inplinearray(ubound(inplinearray))
search_user = strservername
end if
rs.movenext
wend


end function

</SCRIPT>
 

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Exporting and Uploading Mailbox Items using Exchange Web Services using the new ExportItems and UploadItems operations in Exchange 2010 SP1

Two new EWS Operations ExportItems and UploadItems where introduced in Exchange 2010 SP1 that allowed you to do a number of useful things that where previously not possible using Exchange Web Services. Any object that Exchange stores is basically a collection of properties for example a message object is a collection of Message properties, Recipient properties and Attachment properties with a few meta properties that describe the underlying storage thrown in. Normally when using EWS you can access these properties in a number of a ways eg one example is using the strongly type objects such as emailmessage that presents the underlying properties in an intuitive way that's easy to use. Another way is using Extended Properties to access the underlying properties directly. However previously in EWS there was no method to access every property of a message hence there is no way to export or import an item and maintain full fidelity of every property on that item (you could export the...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.