1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
Private function ObtenirMail(byval login as string, byval motdepasse as string, byval groupe as string) as string
try
Dim LDAP_Chemin As String = "LDAP://ou=<mon OU>,dc=<mon DC>,dc=<mon DC>"
' Exemple Dim LDAP_Chemin As String = "LDAP://ou=senyniumcorp,dc=senyniumcorp,dc=lan"
' On renseigne les informations de recherche
Dim chemin As New DirectoryServices.DirectoryEntry
chemin.AuthenticationType = DirectoryServices.AuthenticationTypes.Secure
chemin.Username = login
chemin.Password = motdepasse
chemin.Path = LDAP_Chemin
'Récuprération des informations Active Directory
Dim rech As New System.DirectoryServices.DirectorySearcher(chemin)
Dim result As System.DirectoryServices.SearchResult
Dim mail As String = ""
'Lister les utilisateurs
rech.Filter = ("(objectClass=user)")
For Each result In rech.FindAll()
'S'assurer de l'existence du mail pour eviter
'les comptes systemes
If (result.GetDirectoryEntry().Properties("mail").Value <> "") Then
For i = 0 To result.GetDirectoryEntry().Properties("memberof").Count - 1
tab1 = Split(result.GetDirectoryEntry().Properties("memberof").Item(i), ",")
tab2 = Split(tab1(0), "=")
'Vérification de l'appartenance au groupe
If (tab2(1) = groupe) Then
mail = mail & result.GetDirectoryEntry().Properties("mail").Value
if (i < (result.GetDirectoryEntry().Properties("memberof").Count - 1) then
mail = mail & ";"
end if
end if
next
end if
next
ObtenirMail = mail
Catch ex As Exception
End Try
end function
Private Sub EnvoiMail(byval login as string, byval motdepasse as string, byval groupe as string, ByVal De as string, ByVal sujet as string, ByVal Message as string)
Dim m As New MailMessage
Dim SMTP_SERV = "server"
Try
m.From = De
m.Subject = sujet
SmtpMail.SmtpServer = SMTP_SERV
' Au format mail m.BodyFormat = MailFormat.Html
Dim Adresses as string = ""
Adresses = ObtenirMail(login, motdepasse, groupe)
if (trim(Adresse) <> "") then
m.Body = Message
m.To = Adresses
SmtpMail.Send(m)
end if
Catch ex As Exception
End Try
End Sub
'Exemple d'utilisation
EnvoiMail("monlogin", "Monmotdepasse", "GPE_Administrateurs", "MonMail", "Salut", "Juste un bonjour") |
Partager