Posted By

mecha on 05/17/11


Tagged


Versions (?)

Get a Windows Active Directory User Email Address


 / Published in: C#
 

  1. /// <summary>
  2. /// Gets the user AD email address from Principal Context.
  3. /// </summary>
  4. /// <param name="username">The username.</param>
  5. /// <returns>Returns the email address of AD user, and an empty string if not found.</returns>
  6. public static string GetADUserEmailAddress(string username) {
  7. using (var pctx = new PrincipalContext(ContextType.Domain)) {
  8. using (UserPrincipal up = UserPrincipal.FindByIdentity(pctx, username)) {
  9. return up != null && !String.IsNullOrEmpty(up.EmailAddress) ? up.EmailAddress : string.Empty;
  10. }
  11. }
  12. }

Report this snippet  

You need to login to post a comment.