Retrieving users email address

C

CJM

I'm trying to retrieve the users email address via ADSI in ASP.

So far, I have the following code:

Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
Response.Write oUser.EmailAddress

But I get the following message:

"The directory property cannot be found in the cache. "

Any ideas where I am going wrong?

Thanks
 
J

Jeff Cochran

I'm trying to retrieve the users email address via ADSI in ASP.

So far, I have the following code:

Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
Response.Write oUser.EmailAddress

But I get the following message:

"The directory property cannot be found in the cache. "

Any ideas where I am going wrong?

First guess is you're looking for a directory property that isn't in
the cache... :)

You're going to need to get specific and tell us versions of OS,
Active Directory or not, etc. Also, if the email address isn't in
your directory, you're still not going to get anything.

Jeff
 
R

Richard Mueller [MVP]

CJM said:
I'm trying to retrieve the users email address via ADSI in ASP.

So far, I have the following code:

Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
Response.Write oUser.EmailAddress

But I get the following message:

"The directory property cannot be found in the cache. "

Any ideas where I am going wrong?

Hi,

I find that EmailAddress is not supported by the WinNT provider. You need to
uses the LDAP provider.

EmailAddress is actually a property method that returns the value of the
mail attribute (which also is not supported by WinNT).
 
C

CJM

Jeff Cochran said:
First guess is you're looking for a directory property that isn't in
the cache... :)

You're going to need to get specific and tell us versions of OS,
Active Directory or not, etc. Also, if the email address isn't in
your directory, you're still not going to get anything.

Jeff

Jeff,

My development server is XPSP2 and we are using Active Directory 2003
(Native mode).

All the email address are populated.

Thanks

Chris
 
C

CJM

Richard Mueller said:
Hi,

I find that EmailAddress is not supported by the WinNT provider. You need
to
uses the LDAP provider.

EmailAddress is actually a property method that returns the value of the
mail attribute (which also is not supported by WinNT).

Thanks Richard, but can you point me in the right direction for the LDAP
code. I gather its more involved than just swapping 'WinNT' for 'LDAP' - a
different string entirely?

Cheers

Chris
 
J

Jeff Cochran

Jeff,

My development server is XPSP2 and we are using Active Directory 2003
(Native mode).

All the email address are populated.

In AD? If so, you'll want to use LDAP to retrieve them instead of
WMI.

Jeff
 
C

CJM

Here's the solution I ended up with:

<%@language="vbscript" %>
<%
Option Explicit

Dim objRootDSE, strDNSDomain, objConnection, strQuery
Dim objRecordSet, strName, strDN
Dim strBase, strFilter, strAttributes

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory.
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Properties("User ID") = "domain\username"
objConnection.Properties("Password") = "password"
objConnection.Open "Active Directory Provider"

' Search for all user objects. Sort recordset by DisplayName.
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" &
Session("UID") & "))"
strAttributes = "mail"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

Set objRecordSet = objConnection.Execute(strQuery)

If objRecordSet.EOF Then
response.Write "bolox"
End If

' Loop through results
Do Until objRecordSet.EOF
response.Write "Email Address: " & objRecordSet.Fields("mail")
objRecordSet.MoveNext
Loop

' Clean up.
objConnection.Close
Set objRootDSE = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing

%>


I was told that the mail property is not always reliable, ie may not be
populated, and that the proxyaddresses field would be better, but that
returns a multi-value responsew which I can't seem to handle. Besides, mail
seems to work OK for me...

Thanks

Chris
 
R

Richard Mueller [MVP]

Hi,

Some comments.

1. If you know "mail" is populated, use it.
2. You don't need to specify a userID and password unless you are running
the code with an account that does not have permisssion to read the
attributes.
3. To read a multi-valued attribute, you can loop through the collection
with a "For Each" loop. However, you must account for the possibility that
the attribute will have no values. For example:

' Retrieve a multi-valued attribute .
strAttributes = "proxyAddresses"

Do Until adoRecordset.EOF
colMail = adoRecordset.Fields("proxyAddresses")
If IsEmpty(colMail) Then
' The attribute is empty (has no values).
Response.Write "No Email address"
Else
' The attribute has one or more values - enumerate.
For Each strMail In colMail
Response.Write "Email Address: " & strMail
Next
End If
Loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top