Discussion:
Getting AD email address using ADSI
(too old to reply)
Joe Kaplan
2006-12-08 03:37:58 UTC
Permalink
You need to read the mail attribute. You can definitely do that with ADSI
or .NET (which uses ADSI under the hood if you are talking about
System.DirectoryServices).

Joe K.
--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
Is it possible to retrieve a domain user's email address (for Windows
2003)
using ADSI? We are able to get pretty much all other information except
that.
If it is possible, an example would be much appreciated. If not, do we
need
to use .NET?
itmanager
2006-12-08 04:16:00 UTC
Permalink
Thanks for the reponse. Would you have an example of how to do that?

We are using the following sample code:

Dim dom As Object = GetObject("WinNT://" & domain)

For Each o As Object In dom
If CType(o.Class, String).ToLower = "user" Then
Dim name As String = o.Name
Dim fullName As String = o.FullName
End If
Next
Post by Joe Kaplan
You need to read the mail attribute. You can definitely do that with ADSI
or .NET (which uses ADSI under the hood if you are talking about
System.DirectoryServices).
Joe K.
--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
Is it possible to retrieve a domain user's email address (for Windows
2003)
using ADSI? We are able to get pretty much all other information except
that.
If it is possible, an example would be much appreciated. If not, do we
need
to use .NET?
Richard Mueller
2006-12-08 05:11:55 UTC
Permalink
The mail attribute is not exposed by the WinNT provider. You will need to
use the LDAP provider. You can use ADO to retrieve the value of this
attribute for all users in your domain in a VBScript program. VB would be
very similar. For example:
=============
Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes

Dim strQuery, adoRecordset, strName, strMail



' Setup ADO objects.

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection



' Search entire Active Directory domain.
strBase = "<LDAP://dc=MyDomain,dc=com>"


' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"



' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,mail"



' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False



' Run the query.
Set adoRecordset = adoCommand.Execute


' Enumerate the resulting recordset.
Do Until adoRecordset.EOF

' Retrieve values and display.
strName = adoRecordset.Fields("sAMAccountName").Value

strMail = adoRecordset.Fields("mail").value

Wscript.Echo "NT Name: " & strName & ", Email: " & strMail

' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop



adoRecordset.Close

adoConnection.Close

=========

For more on using ADO to retrieve attribute values see this link:



http://www.rlmueller.net/ADOSearchTips.htm
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
Post by itmanager
Thanks for the reponse. Would you have an example of how to do that?
Dim dom As Object = GetObject("WinNT://" & domain)
For Each o As Object In dom
If CType(o.Class, String).ToLower = "user" Then
Dim name As String = o.Name
Dim fullName As String = o.FullName
End If
Next
Post by Joe Kaplan
You need to read the mail attribute. You can definitely do that with ADSI
or .NET (which uses ADSI under the hood if you are talking about
System.DirectoryServices).
Joe K.
--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
Is it possible to retrieve a domain user's email address (for Windows
2003)
using ADSI? We are able to get pretty much all other information except
that.
If it is possible, an example would be much appreciated. If not, do we
need
to use .NET?
g***@yahoo.com
2006-12-23 01:31:59 UTC
Permalink
This might be helpful:


Public Function getPrimaryEmail(ObjMember)
On Error Resume Next
Primary = "-"
zz = 1
For each email in ObjMember.proxyAddresses
If Left (email,5) = "SMTP:" Then
Primary = Mid (email,6) ' if SMTP is all caps, then it's the
Primary
ElseIf Left (email,5) = "smtp:" Then
Secondary(zz) = Mid (email,6) ' load the list of 2ndary SMTP
emails into Array.
zz = zz + 1
End If
Next
getPrimaryEmail= Primary
End Function

Joe Richards [MVP]
2006-12-08 05:25:23 UTC
Permalink
Or possibly proxyAddresses if using Exchange and a user/group has
multiple email addresses.

joe

--
Joe Richards Microsoft MVP Windows Server Directory Services
Author of O'Reilly Active Directory Third Edition
www.joeware.net


---O'Reilly Active Directory Third Edition now available---

http://www.joeware.net/win/ad3e.htm
Post by Joe Kaplan
You need to read the mail attribute. You can definitely do that with ADSI
or .NET (which uses ADSI under the hood if you are talking about
System.DirectoryServices).
Joe K.
Richard Mueller
2006-12-08 16:58:22 UTC
Permalink
The script can be run on any computer joined to the domain and it should
contact the nearest Domain Controller. You just specify the domain. The
script cannot retrieve user information from a computer that is not joined
to the domain. The script cannot query a standalone server, as it has no
copy of Active Directory, and thus no information about users (except local
users, and local users have no attributes for email).

If you are getting an error, what statement raises the error?
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
One quick question. How can I test this against a standalone Windows 2000
server that is not part of a domain and is not a Domain Controller?
"<LDAP://dc=servername>"
but that threw errors saying it could not find the domain. Can one
substitute just the machine name or IP address instead, or is there some
other syntax available?
Post by Joe Richards [MVP]
Or possibly proxyAddresses if using Exchange and a user/group has
multiple email addresses.
joe
--
Joe Richards Microsoft MVP Windows Server Directory Services
Author of O'Reilly Active Directory Third Edition
www.joeware.net
---O'Reilly Active Directory Third Edition now available---
http://www.joeware.net/win/ad3e.htm
Post by Joe Kaplan
You need to read the mail attribute. You can definitely do that with ADSI
or .NET (which uses ADSI under the hood if you are talking about
System.DirectoryServices).
Joe K.
Joe Richards [MVP]
2006-12-08 18:23:10 UTC
Permalink
Sure, you can access local resources on a standalone but there is no
mail information on a standalone, all of that info is maintained in AD
for Exchange.

--
Joe Richards Microsoft MVP Windows Server Directory Services
Author of O'Reilly Active Directory Third Edition
www.joeware.net


---O'Reilly Active Directory Third Edition now available---

http://www.joeware.net/win/ad3e.htm
The specified domain either does not exist or could not be contacted.
at
"<LDAP://dc=servername>"
When we used the WinNT provider we did not have this problem and could
access local users on a standalone server.
Post by Richard Mueller
The script can be run on any computer joined to the domain and it should
contact the nearest Domain Controller. You just specify the domain. The
script cannot retrieve user information from a computer that is not joined
to the domain. The script cannot query a standalone server, as it has no
copy of Active Directory, and thus no information about users (except local
users, and local users have no attributes for email).
If you are getting an error, what statement raises the error?
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
One quick question. How can I test this against a standalone Windows 2000
server that is not part of a domain and is not a Domain Controller?
"<LDAP://dc=servername>"
but that threw errors saying it could not find the domain. Can one
substitute just the machine name or IP address instead, or is there some
other syntax available?
Post by Joe Richards [MVP]
Or possibly proxyAddresses if using Exchange and a user/group has
multiple email addresses.
joe
--
Joe Richards Microsoft MVP Windows Server Directory Services
Author of O'Reilly Active Directory Third Edition
www.joeware.net
---O'Reilly Active Directory Third Edition now available---
http://www.joeware.net/win/ad3e.htm
Post by Joe Kaplan
You need to read the mail attribute. You can definitely do that with ADSI
or .NET (which uses ADSI under the hood if you are talking about
System.DirectoryServices).
Joe K.
Joe Richards [MVP]
2006-12-08 18:24:23 UTC
Permalink
If you just want a dump of all SMTP addresses in your org, just do
something like this

adfind -sc exchaddresses:smtp

You can get adfind from

http://www.joeware.net/win/free/tools/adfind.htm

--
Joe Richards Microsoft MVP Windows Server Directory Services
Author of O'Reilly Active Directory Third Edition
www.joeware.net


---O'Reilly Active Directory Third Edition now available---

http://www.joeware.net/win/ad3e.htm
Thanks for all the help. We will try this out.
Post by Joe Richards [MVP]
Or possibly proxyAddresses if using Exchange and a user/group has
multiple email addresses.
joe
--
Joe Richards Microsoft MVP Windows Server Directory Services
Author of O'Reilly Active Directory Third Edition
www.joeware.net
---O'Reilly Active Directory Third Edition now available---
http://www.joeware.net/win/ad3e.htm
Post by Joe Kaplan
You need to read the mail attribute. You can definitely do that with ADSI
or .NET (which uses ADSI under the hood if you are talking about
System.DirectoryServices).
Joe K.
Loading...