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

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

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 Graph is limited to a m

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
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.