C# Outlook帐户-如何获取Exchange电子邮件地址?

C# Outlook帐户-如何获取Exchange电子邮件地址?,c#,exchange-server,office-interop,outlook-addin,C#,Exchange Server,Office Interop,Outlook Addin,我反复浏览Outlook中的所有电子邮件帐户。 我可以获取POP和IMAP帐户的电子邮件地址,但无法获取MS Exchange帐户的电子邮件地址 这是我的密码: foreach (Outlook.Account account in accounts) { Debug.Print("DisplayName: " + account.DisplayName); Debug.Print("UserName: " + account.UserName);

我反复浏览Outlook中的所有电子邮件帐户。 我可以获取POP和IMAP帐户的电子邮件地址,但无法获取MS Exchange帐户的电子邮件地址

这是我的密码:

    foreach (Outlook.Account account in accounts) {
        Debug.Print("DisplayName: " + account.DisplayName);
        Debug.Print("UserName: " + account.UserName);
        Debug.Print("CurrentUser.Address: " + account.CurrentUser.Address); //email address or long Exchange address
        Debug.Print("CurrentUser.Name: " + account.CurrentUser.Name);
        Debug.Print("CurrentUser.AddressEntry.Address: " + account.CurrentUser.AddressEntry.Address);
        Debug.Print("DeliveryStore.DisplayName: " + account.DeliveryStore.DisplayName);
    }
以下是非外汇账户的结果:

DisplayName: Edgar Test Account
UserName: Edgar Tester
CurrentUser.Address: edgar@testtest.com
CurrentUser.Name: Edgar Tester
CurrentUser.AddressEntry.Address: edgar@testtest.com
DeliveryStore.DisplayName: Edgar Tester
作为交换:

DisplayName: User Name
UserName: 000300KJDFA6220
CurrentUser.Address: /O=FIRST ORGANIZATION/OU=EXCHANGE ADMINISTRATIVE GROUP(FYDIAASFDSFLT)/CN=RECIPIENTS/CN=00458787FA6220
CurrentUser.Name: /O=FIRST ORGANIZATION/OU=EXCHANGE ADMINISTRATIVE GROUP(FYDLKHHKLSPDLT)/CN=RECIPIENTS/CN=0054654DFA6220
CurrentUser.AddressEntry.Address: /O=FIRST ORGANIZATION/OU=EXCHANGE ADMINISTRATIVE GROUP(FYDIDADS23SPDLT)/CN=RECIPIENTS/CN=0058876220
DeliveryStore.DisplayName: User Name
(我更改了一些数字和字母以匿名)

我想要Exchange帐户的电子邮件地址。我怎样才能得到这个呢?

试试:


稍微调整一下参数。

对于Exchange帐户,使用
account.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress

从示例中可以看出,所有Exchange帐户的
DeliveryStore.DisplayName
都等于
DisplayName
。对于非外汇账户来说,这总是对的还是错的?@Atterson:我很确定这些名字是相互独立的。但如果用户使用默认名称,则是相同的。我真的不在乎这个名字。我要电子邮件地址!是的,我只是按照某种思路来回答。我确认这适用于“真实”的MS Exchange帐户。到目前为止,我还以为MS live.com帐户的工作方式与Exchange帐户类似,但事实并非如此,至少在这里不是这样。以上代码显示MS live.com帐户出现错误!是的,GetExchangeUser()将为非EX地址项返回nullxxx@live.com账户。对于一个帐户,CurrentUser.AddressEntry.GetExchangeUser()返回一个值,我可以使用上面的代码。另一方面yyy@live.comaccount CurrentUser.AddressEntry.GetExchangeUser()返回NULL。知道为什么以及如何“修复”这个问题吗?在这种情况下,您可以使用CurrentUser.Address
//ExchangeService service; //you have some service in your code.

NameResolutionCollection nameColl = service.ResolveName(CurrentUser.Name, ResolveNameSearchLocation.DirectoryOnly,true);