Get current user's email address in .NET

Calv1n picture Calv1n · Sep 9, 2011 · Viewed 41.8k times · Source

I would like to know the email address of the user (assuming she's in a typical Windows office network). This is in a C# application. Perhaps something to the effect of

CurrentUser.EmailAddress; 

Answer

Colonel Panic picture Colonel Panic · Mar 13, 2012

Reference System.DirectoryServices.AccountManagement, then

using System.DirectoryServices.AccountManagement;
UserPrincipal.Current.EmailAddress

Or with a timeout:

var task = Task.Run(() => UserPrincipal.Current.EmailAddress);
if (task.Wait(TimeSpan.FromSeconds(1)))
    return task.Result;