Defining a Unique Email Address and Validating Mail Suffix

In addition to creating a valid and unique email address – as defined here: https://blog.oholics.net/defining-a-unique-email-address/

I also need to detect where those addresses might need to be changed. The AD and Exchange infrastructure supports a number of different tenant organisations, each with their own needs. There is regular horizontal movement of users between these organisations.

So I have added to my code to define a unique email address – the additional content starts at Line 72 – statement “If mventry.Item(“mail”).IsPresent Then“. Note that attributes are set on the import from HR to define who is entitled to an MBX and those who should just be mail enabled. The current code just logs out those things for action, but I have also included making these events throw an exception to the Sync Engine.

It was interesting to see just how many people are not really entitled to a mailbox, but who have one anyway!

Case "mail-ADMA-Export"
'MV attributes required are: provisionExchangeMailbox, firstName, sn, initials, company, orgSite, department
'Pump out attribute required for mailbox provision
If mventry.Item("provisionExchangeMailbox").IsPresent Then
If Not csentry.Item("mail").IsPresent Then 'Mail attribute is not present
If mventry.Item("provisionExchangeMailbox").Value.ToLower = "true" Then
'Lets start trying to generate a new email address for this user...
'Start calculating the suffix:
Dim mailSuffix As String = ""
Dim mailPrefix As String = ""
If mventry.Item("firstName").IsPresent And mventry.Item("sn").IsPresent Then
If mventry.Item("orgSite").IsPresent Then
If mventry.Item("orgSite").Value.ToLower = "oxford" Or mventry.Item("orgSite").Value.ToLower = "edinburgh" Then
mailSuffix = "@abcd.co.uk"
End If
End If
If mventry.Item("company").IsPresent Then
If mventry.Item("company").Value.ToLower = "abcd" Then
mailSuffix = "@abcd.co.uk"
ElseIf mventry.Item("company").Value.ToLower = "lmno" Then
mailSuffix = "@lmno.co.uk"
ElseIf mventry.Item("company").Value.ToLower = "wxyz" Then
mailSuffix = "@wxyz.co.uk"
End If
End If
If mventry.Item("department").IsPresent Then
If mventry.Item("department").Value.ToLower = "lmno" Then
mailSuffix = "@lmno.co.uk"
ElseIf mventry.Item("department").Value.ToLower = "pqrs" Then
mailSuffix = "@pqrs.co.uk"
End If
End If
'Next generate a prefix...
'1. firstname.lastname
mailPrefix = mventry.Item("firstName").Value & "." & mventry.Item("sn").Value
'Then go check if it has already been used by looking in the MV.
Dim AdminEntry1() As MVEntry = Utils.FindMVEntries("mail", mailPrefix & mailSuffix)
If AdminEntry1.Length = 0 Then
csentry.Item("mail").Value = mailPrefix & mailSuffix
ElseIf AdminEntry1.Length <> 0 Then
'That address is already used, so lets try creating a new prefix...
'2. firstname.initial.lastname
mailPrefix = mventry.Item("firstName").Value & "." & mventry.Item("initials").Value & "." & mventry.Item("sn").Value
Dim AdminEntry2() As MVEntry = Utils.FindMVEntries("mail", mailPrefix & mailSuffix)
If AdminEntry2.Length = 0 Then
csentry.Item("mail").Value = mailPrefix & mailSuffix
ElseIf AdminEntry2.Length <> 0 Then
'That address is already used, so lets try creating a new prefix...
'3. initials.lastname
mailPrefix = mventry.Item("initials").Value & "." & mventry.Item("sn").Value
Dim AdminEntry3() As MVEntry = Utils.FindMVEntries("mail", mailPrefix & mailSuffix)
If AdminEntry3.Length = 0 Then
csentry.Item("mail").Value = mailPrefix & mailSuffix
ElseIf AdminEntry3.Length <> 0 Then
'That address is already used, so lets try creating a new prefix...
'4. firstname.lastname<n>
Dim rnd As New Random()
Dim prefixNum As Integer = rnd.Next(1, 9)
mailPrefix = mventry.Item("firstName").Value & "." & mventry.Item("sn").Value & prefixNum
Dim AdminEntry4() As MVEntry = Utils.FindMVEntries("mail", mailPrefix & mailSuffix)
If AdminEntry4.Length = 0 Then
csentry.Item("mail").Value = mailPrefix & mailSuffix
ElseIf AdminEntry4.Length <> 0 Then
Throw New Exception("Unable to allocate Email Address")
End If
End If
End If
End If
End If
End If
End If
If mventry.Item("mail").IsPresent Then
If mventry.Item("provisionExchangeMailbox").Value.ToLower = "true" Then
'User is still entitled to an MBX...Calculate what suffix the user should have:
Dim mailSuffix As String = ""
If mventry.Item("orgSite").IsPresent Then
If mventry.Item("orgSite").Value.ToLower = "oxford" Or mventry.Item("orgSite").Value.ToLower = "edinburgh" Then
mailSuffix = "@abcd.co.uk"
End If
End If
If mventry.Item("company").IsPresent Then
If mventry.Item("company").Value.ToLower = "abcd" Then
mailSuffix = "@abcd.co.uk"
ElseIf mventry.Item("company").Value.ToLower = "lmno" Then
mailSuffix = "@lmno.co.uk"
ElseIf mventry.Item("company").Value.ToLower = "wxyz" Then
mailSuffix = "@wxyz.co.uk"
End If
End If
If mventry.Item("department").IsPresent Then
If mventry.Item("department").Value.ToLower = "lmno" Then
mailSuffix = "@lmno.co.uk"
ElseIf mventry.Item("department").Value.ToLower = "pqrs" Then
mailSuffix = "@pqrs.co.uk"
End If
End If
'Does the suffix match?
If mventry.Item("mail").Value.ToLower.IndexOf(mailSuffix.ToLower) = -1 Then
'the suffix does not match, so raise an error!
'Throw New Exception("Wrong email suffix for user with email address: " & mventry("mail").Value & " , suffix should be " & mailSuffix)
Logging.Log("Wrong email suffix for user with email address: " & mventry("mail").Value & ", suffix should be " & mailSuffix, True, 0)
End If
Else
If mventry("mailEnable").IsPresent Then
If mventry("mailEnable").Value = "False" Then 'covers/ ignores island site people
'User is not entitled to an MBX...
Logging.Log("User no longer entitled to an MBX," & csentry("mail").Value, True, 0)
End If
End If
End If
End If
End If