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...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...
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.