45

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; 
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
Calv1n
  • 623
  • 1
  • 5
  • 7
  • Please clarify your question. Are you extending exchange server through some kind of API? Is this a standalone application that connects to exchange and tries to find out when the "certain condition" is met? You're not giving us much to work with here. – feathj Sep 09 '11 at 04:35
  • I'm pretty sure the OP is talking about AD. However, Calv1n, you should probably clarify your question by editing it, or the community is likely to close it. – Tim Post Sep 09 '11 at 12:57

5 Answers5

141

Reference System.DirectoryServices.AccountManagement, then

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

See .NET docs UserPrincipal.Current and UserPrincipal.EmailAddress.


Or with a timeout:

var task = Task.Run(() => UserPrincipal.Current.EmailAddress);
if (task.Wait(TimeSpan.FromSeconds(1)))
    return task.Result;
    
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • 11
    This should be the accepted answer. Much, much simpler than the linked article. – Matt Honeycutt Jan 21 '14 at 19:54
  • 1
    UserPrincipal.Current is very slow on my non-domain computer. Seems like a timeout of around 5 seconds before that property returns and I have yet not found a way to work around that. – angularsen Apr 06 '15 at 14:59
  • 1
    Important: This is VERY slow if the user it not on a domain. One of those issues your customers will see and not you. – Robin Apr 08 '16 at 15:47
  • In addition - a non-domain computer receives an exception of "FileNotFound". – David Ford Apr 11 '16 at 06:56
  • UserPrincipal.Current does not work on .NET, it throws PlatformNotSupportedException even on Windows OS. It only works on .NET Framework; see documentation: https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.userprincipal.current?view=netframework-4.8 – daniol Aug 25 '23 at 06:50
5

If you're behind a Windows domain, you could always grab their email address out of Active Directory.

See Javier G. Lozano's example in his tutorial, "Querying Active Directory for User Emails".

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
Ryan
  • 92
  • 1
  • 3
1
// Simply by using UserPrincipal
// Include the namespace - System.DirectoryServices

using DS = System.DirectoryServices;
string CurrUsrEMail = string.Empty;
CurrUsrEMail = DS.AccountManagement.UserPrincipal.Current.EmailAddress;
hellow
  • 12,430
  • 7
  • 56
  • 79
fengfu bon
  • 11
  • 1
  • 3
  • 2
    While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. Please edit your answer. – hellow Aug 28 '18 at 06:30
1

I didn't want to use the Active Directory option and the other, most selected answer, did not work for me oddly enough.

I searched my code bank and found this which worked fine and with quick response:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "[domain]",  dc=xx,dc=yyy"))
{
    UserPrincipal cp = UserPrincipal.FindByIdentity(ctx, Environment.UserName);
    userEmail = cp.EmailAddress;
}
BillJam
  • 221
  • 3
  • 13
0

No doubt many are fine using Directory Services (LDAP etc.), however for those not able (or willing) this might work as an alternative.

PM> Install-Package Microsoft.Office.Interop.Outlook -Version 15.0.4797.1003

using MSOutlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

...

private async Task<string> GetCurrentUserEmailAsync()
{
    var value = string.Empty;
    MSOutlook.Application outlook = new MSOutlook.Application();
    await Task.Delay(3000);
    MSOutlook.AddressEntry addrEntry =
        outlook.Session.CurrentUser.AddressEntry;
    if (addrEntry.Type == "EX")
    {
        MSOutlook.ExchangeUser currentUser =
            outlook.Session.CurrentUser.
            AddressEntry.GetExchangeUser();
        if (currentUser != null)
        {
            value = currentUser.PrimarySmtpAddress;
        }
        Marshal.ReleaseComObject(currentUser);
    }
    Marshal.ReleaseComObject(addrEntry);
    Marshal.ReleaseComObject(outlook);
    return value;
}
David Savage
  • 314
  • 4
  • 13