Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Generic Array From File Function To Cope With Inevitable Exceptions
Case "emailAddressPresent-ADMA-Import"
'AD attributes required: mail and msExchHomeServerName
' Default setting = False
mventry("emailAddressPresent").Value = "False"
If csentry("mail").IsPresent And csentry("msExchHomeServerName").IsPresent Then
Dim suffix() As String = Split((csentry("mail").Value), "@") 'mail.Split("@")
'Valid/allowed email suffixes are defined in the following array (amend as appropriate):
Dim validMailAddresses() As String = {"blah.ac.uk", "foo.ac.uk", "bar.ac.uk", "otherorg.ac.uk"}
If (Array.IndexOf(validMailAddresses, suffix(1).ToLower) <> -1) Then
mventry("emailAddressPresent").Value = "True"
ElseIf generateArrayFromFile("C:\FIMControl\AdditionalValidMailSuffixes.txt").Contains(suffix(1).ToLower) Then ' Extra suffixes can be added to the text file defined here
mventry("emailAddressPresent").Value = "True"
Else
'If a suffix from the above is not found - raise an error, so that the suffix can be added to the array/ text file or simply sorted out - where a mistake was made.
Throw New Exception("Invalid email suffix found: " & suffix(1))
End If
ElseIf csentry("mail").IsPresent And csentry("mailNickName").IsPresent Then
'This person is a mail enabled user, maybe with an island site email address or just something else so we want them to be able to be added to distribution lists....
mventry("emailAddressPresent").Value = "True"
End If
Public Function generateArrayFromFile(ByVal file As String) As ArrayList
Dim arrayFromFile As New ArrayList()
Try
Dim reader As New System.IO.StreamReader(file)
While Not (reader.Peek() = -1)
arrayFromFile.Add(reader.ReadLine())
End While
reader.Close()
reader.Dispose()
Catch ex As IOException
arrayFromFile.Add(ex.ToString())
End Try
Return arrayFromFile
End Function
'Does the suffix match?
If mventry.Item("mail").Value.ToLower.IndexOf(mailSuffix.ToLower) = -1 Then
'the suffix does not match, so raise an error..... Unless, the email address (in lower case) is in the text file referenced below...
If Not generateArrayFromFile("C:\FIMControl\IgnoreEmailAddressErrors.txt").Contains(mventry("mail").Value.ToLower) Then
Throw New Exception("Wrong email suffix for user with email address: " & mventry("mail").Value & " , suffix should be " & mailSuffix)
End If
End If
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment