locked
How do I send mail from my ILM extensions RRS feed

  • General discussion

  •   ILM Knowledge Bit

    Very often it is necessary to alert administrators or other groups of people of events happening during the execution of ILM rules. You may e.g. alert an administrator that a new user was provisioned and that he needs to follow up with some manual action.

    An approach that could be followed is to create a template containing the text of your mail message, in which you allow the replacement of some variables at run-time (in the example below: %%1, %%2 and %%3). A file such as the following could be used:

    Dear Administrator,
    
    An Active Directory user with DN: %%1 (%%2 %%3) was created. Please execute the necessary actions in AD to finalize creating this user.
    
    Regards,
    Your friendly ILM server
    
    Note: this mail is generated by an automated system, so please, do not reply to this e-mail!
    

    To use this template during provisioning, the easiest way is to initialize a variable containing the entire file in the Initialize method of your MVExtension:

      Private sProvisioningAlertTxt As String = ""
    
      Public Sub Initialize() _
      Implements IMVSynchronization.Initialize
    
        ' Initialize the mail message texts that is sent when a user is renamed
        Dim sr As StreamReader = New StreamReader(Utils.ExtensionsDirectory & "\" & "myTemplate.txt")
        Dim Line As String
    
        Do
          Line = sr.ReadLine()
          If Not Line Is Nothing Then
            sProvisioningAlertTxt += Line + vbCrLf
          End If
        Loop Until Line Is Nothing
    
      End Sub
    

    You can then implement in your MVExtension a method that sends a mail using this text file:

      Private Sub SendProvisionMail( _
        ByVal mventry As MVEntry, _
        ByVal sDN As String _
      )
    
        ' %%1 will be replaced by the subject's DN
        ' %%2 will be replaced by the subject's givenName
        ' %%3 will be replaced by the subject's sn
    
        Dim email As New MailMessage
        Dim sMailTxt As String = sProvisioningAlertTxt
        Dim smtp As New SmtpClient("smtp.mydomain.com")
    
        email.From = New MailAddress("mail.from@mydomain.com"), "Friendly From"))
    
        email.To.Add("ProvisioningToDL@mydomain.com")
    
        email.Subject = MAParms("A New User was Provisioned by ILM")
    
        sMailTxt = Regex.Replace(sMailTxt, "%%1", sDN)
        If mventry("givenName").IsPresent Then
          sMailTxt = Regex.Replace(sMailTxt, "%%2", mventry("givenName").Value)
        Else
          sMailTxt = Regex.Replace(sMailTxt, "%%2", "Unknown")
        End If
        If mventry("sn").IsPresent Then
          sMailTxt = Regex.Replace(sMailTxt, "%%3", mventry("sn").Value)
        Else
          sMailTxt = Regex.Replace(sMailTxt, "%%3", "Unknown")
        End If
        email.Body = sMailTxt
    
        email.IsBodyHtml = False
        email.Priority = MailPriority.High
    
        ' send the mail only if we are really executing code!
        If (Not Utils.InPreviewMode) Then
          smtp.Send(email)
        End If
    
      End Sub
    

    In your provisioning code, you could then send a mail to your administrator by simply adding the following code:

        ...
    
        csentry.CommitNewConnector()
        ' if committing the connector happened, a new object user was provisioned and we can thus
        ' inform the AD Administrators
        SendProvisionMail(mventry, sDN)
    
        ...
    

    Obviously, the above code leaves a lot of room for improvement, but it presents a basic framework to get the job done. Also, don't forget to reference the necessary libraries in your MVExtension assembly. You will need at least the following:

    • System.Text.RegularExpressions
    • System.io
    • System
    • System.Net.Mail

     

      Go to the ILM Knowledge Bit Collection
    Sunday, June 20, 2010 7:42 PM