C# 获取当前用户';在.NET中的电子邮件地址

C# 获取当前用户';在.NET中的电子邮件地址,c#,.net,exchange-server,C#,.net,Exchange Server,我想知道用户的电子邮件地址(假设她在典型的Windows office网络中)。这是一个C#应用程序。也许有点像 CurrentUser.EmailAddress; 如果您在Windows域后面,您可以随时从Active Directory中获取他们的电子邮件地址 参见Javier G.Lozano在其教程中的示例,“”参考System.DirectoryServices.AccountManagement,然后 using System.DirectoryServices.AccountM

我想知道用户的电子邮件地址(假设她在典型的Windows office网络中)。这是一个C#应用程序。也许有点像

CurrentUser.EmailAddress; 

如果您在Windows域后面,您可以随时从Active Directory中获取他们的电子邮件地址


参见Javier G.Lozano在其教程中的示例,“”

参考
System.DirectoryServices.AccountManagement
,然后

using System.DirectoryServices.AccountManagement;
return UserPrincipal.Current.EmailAddress;
请参阅.NET文档和


或超时:

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

我不想使用Active Directory选项,而另一个选择最多的答案对我来说也不够奇怪

我搜索了我的代码库,发现它运行良好,响应迅速:

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

毫无疑问,许多人可以使用目录服务(LDAP等),但是对于那些不能(或不愿意)使用目录服务的人来说,这可能是一种替代方法

PM>安装软件包Microsoft.Office.Interop.Outlook-版本15.0.4797.1003

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

专用异步任务GetCurrentUserEmailAsync()
{
var值=string.Empty;
MSOutlook.Application outlook=新建MSOutlook.Application();
等待任务。延迟(3000);
MSOutlook.AddressEntry addrEntry=
outlook.Session.CurrentUser.AddressEntry;
if(addrEntry.Type==“EX”)
{
MSOutlook.ExchangeUser当前用户=
outlook.Session.CurrentUser。
AddressEntry.GetExchangeUser();
如果(currentUser!=null)
{
值=currentUser.PrimarySmtpAddress;
}
Marshal.ReleaseComObject(当前用户);
}
Marshal.ReleaseComObject(addrEntry);
Marshal.ReleaseComObject(outlook);
返回值;
}

您的问题不清楚,而且没有明确说明。你能再说一遍吗?请澄清你的问题。您是否通过某种API扩展exchange server?这是一个独立的应用程序,它连接到exchange并尝试找出何时满足“特定条件”?你在这里没有给我们太多的工作机会。我很确定OP是在谈论广告。但是,Calv1n,你可能应该通过编辑来澄清你的问题,否则社区可能会关闭它。这应该是公认的答案。比链接的article.UserPrincipal.Current简单得多,在我的非域计算机上速度非常慢。在该属性返回之前,似乎有大约5秒的超时时间,我还没有找到解决方法。重要提示:如果用户不在域中,这将非常缓慢。其中一个问题是你的客户会看到,而不是你。此外,非域计算机会收到“FileNotFound”异常。虽然这可能会回答作者的问题,但它缺少一些解释性词语和文档链接。如果没有一些短语,原始代码片段就没有多大帮助。请编辑您的答案。这是LDAP,即Active Directory。
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;
}