[python-win32] Getting the current user's email address

Jens B. Jorgensen jens.jorgensen@tallan.com
Thu, 12 Dec 2002 09:51:04 -0600

Actually I wouldn't think this would be simple at all. Why would an IIS 
server know the email address of the user that connected to it? If the 
server is part of a native win2k domain with Active Directory then the 
directory service *might* have the user's email address. Which is to say 
that by coincidence user principal name might be the same as the user's 
email address. That's what you can get from GetUserNameEx but is not the 
same as a user's email address even though it looks like one. On the 
other hand if you're running Exchange you may be able to get the user's 
email address through the ADSI interface to Exchange. If your Domain 
user names are mapped 1-to-1 to Mailbox names in exchange then you 
should be able to get the email address with:

adsi_obj = GetObject('LDAP://<exchange server>/cn=<mailbox name (nt user 
name)>,cn=Recipients,ou=<whatever your ou is>,o=<whatever was set as the 
organization name>')
email_addr = adsi_obj.Get('mail')

The code would be similar if you're lookup up the user in Active 
Directory. I don't have AD set up so I couldn't say exactly what the DN 
would need to look like, probably something similar to the above minues 
the 'cn=Recipients' part.

Don Dwiggins wrote:

>It seems that this ought to be relatively simple, but I've been frustrated
>at every turn I've tried.  I'm running under an ASP process under IIS, which
>is impersonating an intranet web client, and I want to get the user's email
>I can get the user's login name with win32api.GetUserName, but I haven't
>succeeded in getting from there to having ADSI get me to the right object to
>ask for the EmailAddress.
>If GetUserNameEx were provided in win32api, I'd be home free -- but it's not
>(at least not in the 2.2.1 ActiveState distro).  I've looked at the Python
>ADSI documentation, but I can't decipher it enough to figure out how to get
>to the user's entry, given just the login name (I haven't seen any way to
>get the password, and I'm not sure it would help).
>Anyone have any good words?

Jens B. Jorgensen

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"