Retrieving users email address

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CJM

    Retrieving users email address

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

    So far, I have the following code:

    Set oUser = GetObject("WinN T://domain/" & Request.Form("U ID") & ",user")
    Response.Write oUser.EmailAddr ess

    But I get the following message:

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

    Any ideas where I am going wrong?

    Thanks


  • Jeff Cochran

    #2
    Re: Retrieving users email address

    On Fri, 19 Nov 2004 16:33:30 -0000, "CJM"
    <cjmnews04@news groups.nospam> wrote:
    [color=blue]
    >I'm trying to retrieve the users email address via ADSI in ASP.
    >
    >So far, I have the following code:
    >
    >Set oUser = GetObject("WinN T://domain/" & Request.Form("U ID") & ",user")
    >Response.Wri te oUser.EmailAddr ess
    >
    >But I get the following message:
    >
    >"The directory property cannot be found in the cache. "
    >
    >Any ideas where I am going wrong?[/color]

    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

    Comment

    • Richard Mueller [MVP]

      #3
      Re: Retrieving users email address

      CJM wrote:
      [color=blue]
      > I'm trying to retrieve the users email address via ADSI in ASP.
      >
      > So far, I have the following code:
      >
      > Set oUser = GetObject("WinN T://domain/" & Request.Form("U ID") & ",user")
      > Response.Write oUser.EmailAddr ess
      >
      > But I get the following message:
      >
      > "The directory property cannot be found in the cache. "
      >
      > Any ideas where I am going wrong?[/color]

      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).

      --
      Richard
      Microsoft MVP Scripting and ADSI
      HilltopLab web site - http://www.rlmueller.net
      --


      Comment

      • CJM

        #4
        Re: Retrieving users email address


        "Jeff Cochran" <jeff.nospam@zi na.com> wrote in message
        news:419f294f.1 804853622@msnew s.microsoft.com ...[color=blue]
        > On Fri, 19 Nov 2004 16:33:30 -0000, "CJM"
        > <cjmnews04@news groups.nospam> wrote:
        >[color=green]
        >>I'm trying to retrieve the users email address via ADSI in ASP.
        >>
        >>So far, I have the following code:
        >>
        >>Set oUser = GetObject("WinN T://domain/" & Request.Form("U ID") & ",user")
        >>Response.Writ e oUser.EmailAddr ess
        >>
        >>But I get the following message:
        >>
        >>"The directory property cannot be found in the cache. "
        >>
        >>Any ideas where I am going wrong?[/color]
        >
        > 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[/color]

        Jeff,

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

        All the email address are populated.

        Thanks

        Chris


        Comment

        • CJM

          #5
          Re: Retrieving users email address


          "Richard Mueller [MVP]" <rlmueller-NOSPAM@ameritec h.NOSPAM.net> wrote in
          message news:%23OcQ9Vmz EHA.3376@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          >
          > 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).
          >
          > --
          > Richard[/color]

          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


          Comment

          • Jeff Cochran

            #6
            Re: Retrieving users email address

            On Tue, 23 Nov 2004 12:39:53 -0000, "CJM"
            <cjmnews04@news groups.nospam> wrote:
            [color=blue]
            >
            >"Jeff Cochran" <jeff.nospam@zi na.com> wrote in message
            >news:419f294f. 1804853622@msne ws.microsoft.co m...[color=green]
            >> On Fri, 19 Nov 2004 16:33:30 -0000, "CJM"
            >> <cjmnews04@news groups.nospam> wrote:
            >>[color=darkred]
            >>>I'm trying to retrieve the users email address via ADSI in ASP.
            >>>
            >>>So far, I have the following code:
            >>>
            >>>Set oUser = GetObject("WinN T://domain/" & Request.Form("U ID") & ",user")
            >>>Response.Wri te oUser.EmailAddr ess
            >>>
            >>>But I get the following message:
            >>>
            >>>"The directory property cannot be found in the cache. "
            >>>
            >>>Any ideas where I am going wrong?[/color]
            >>
            >> 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[/color]
            >
            >Jeff,
            >
            >My development server is XPSP2 and we are using Active Directory 2003
            >(Native mode).
            >
            >All the email address are populated.[/color]

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

            Jeff

            Comment

            • Jeff Cochran

              #7
              Re: Retrieving users email address

              On Tue, 23 Nov 2004 12:42:03 -0000, "CJM"
              <cjmnews04@news groups.nospam> wrote:
              [color=blue]
              >
              >"Richard Mueller [MVP]" <rlmueller-NOSPAM@ameritec h.NOSPAM.net> wrote in
              >message news:%23OcQ9Vmz EHA.3376@TK2MSF TNGP12.phx.gbl. ..[color=green]
              >>
              >> 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).
              >>
              >> --
              >> Richard[/color]
              >
              >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?[/color]

              Lots of sampels availble, try:

              eSports News, Results, upcoming Matches & live Matches. Learn tricks and guides in the esports space. ✅ We cover CS:GO, Dota 2, LOL, Overwatch & PUBG. 


              Jeff

              Comment

              • CJM

                #8
                Re: Retrieving users email address

                Here's the solution I ended up with:

                <%@language="vb script" %>
                <%
                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( "defaultNamingC ontext")

                ' Use ADO to search Active Directory.
                Set objConnection = CreateObject("A DODB.Connection ")
                objConnection.P rovider = "ADsDSOObje ct"
                objConnection.P roperties("User ID") = "domain\usernam e"
                objConnection.P roperties("Pass word") = "password"
                objConnection.O pen "Active Directory Provider"

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

                Set objRecordSet = objConnection.E xecute(strQuery )

                If objRecordSet.EO F Then
                response.Write "bolox"
                End If

                ' Loop through results
                Do Until objRecordSet.EO F
                response.Write "Email Address: " & objRecordSet.Fi elds("mail")
                objRecordSet.Mo veNext
                Loop

                ' Clean up.
                objConnection.C lose
                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



                Comment

                • Richard Mueller [MVP]

                  #9
                  Re: Retrieving users email address

                  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.EO F
                  colMail = adoRecordset.Fi elds("proxyAddr esses")
                  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

                  --
                  Richard
                  Microsoft MVP Scripting and ADSI
                  HilltopLab web site - http://www.rlmueller.net
                  --

                  "CJM" <cjmnews04@news groups.nospam> wrote in message
                  news:eNGVU3W0EH A.1860@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > Here's the solution I ended up with:
                  >
                  > <%@language="vb script" %>
                  > <%
                  > 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( "defaultNamingC ontext")
                  >
                  > ' Use ADO to search Active Directory.
                  > Set objConnection = CreateObject("A DODB.Connection ")
                  > objConnection.P rovider = "ADsDSOObje ct"
                  > objConnection.P roperties("User ID") = "domain\usernam e"
                  > objConnection.P roperties("Pass word") = "password"
                  > objConnection.O pen "Active Directory Provider"
                  >
                  > ' Search for all user objects. Sort recordset by DisplayName.
                  > strBase = "<LDAP://" & strDNSDomain & ">"
                  > strFilter = "(&(objectCateg ory=person)(obj ectClass=user)( sAMAccountName= "[/color]
                  &[color=blue]
                  > Session("UID") & "))"
                  > strAttributes = "mail"
                  > strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
                  >
                  > Set objRecordSet = objConnection.E xecute(strQuery )
                  >
                  > If objRecordSet.EO F Then
                  > response.Write "bolox"
                  > End If
                  >
                  > ' Loop through results
                  > Do Until objRecordSet.EO F
                  > response.Write "Email Address: " & objRecordSet.Fi elds("mail")
                  > objRecordSet.Mo veNext
                  > Loop
                  >
                  > ' Clean up.
                  > objConnection.C lose
                  > 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,[/color]
                  mail[color=blue]
                  > seems to work OK for me...
                  >
                  > Thanks
                  >
                  > Chris
                  >
                  >
                  >[/color]


                  Comment

                  Working...