获取多个帐户的信息

Microsoft Outlook 支持包含连接到 Microsoft Exchange Server 的一个或多个帐户的配置文件。 本主题演示如何获取和显示有关当前配置文件中每个帐户的其他信息。

以下方法 EnumerateAccounts显示当前配置文件中定义的每个帐户的帐户名、用户名和简单邮件传输协议 (SMTP) 地址。 如果帐户已连接到 Exchange 服务器, EnumerateAccounts 则显示 Exchange 服务器名称和版本信息。 如果帐户驻留在传递存储中, EnumerateAccounts 则显示该帐户的默认传递存储的名称。

EnumerateAccounts 会从 Account 对象中访问大部分信息,但在 Account 对象不包含有关用户名和 SMTP 地址的信息时除外。 在这种情况下, EnumerateAccounts 使用 AddressEntryExchangeUser 对象。 EnumerateAccounts使用从 Account.CurrentUser 属性获取的 Recipient 对象的 AddressEntry 属性获取 AddressEntry 对象。 EnumerateAccounts使用 AddressEntry 对象的 GetExchangeUser 方法获取 ExchangeUser 对象。 下面是使用 AccountAddressEntryExchangeUser 对象获取各种信息的算法:

  • 如果 Account 对象包含有关用户名和 SMTP 地址的信息,那么使用 Account 对象显示帐户名、用户名和 SMTP 地址,如果帐户是 Exchange 帐户,那么还将显示 Exchange 服务器名称和版本信息。

  • 否则,如果 Account 对象不包含有关用户名和 SMTP 地址的信息,那么将如下进行处理:

    • 如果帐户不是 Exchange 帐户,那么使用 AddressEntry 对象显示用户名和 SMTP 地址。

    • 否则,如果帐户为 Exchange 帐户,那么如下进行处理:

      1. 使用 Account 对象显示帐户名、Exchange 服务器名、Exchange 版本信息。

      2. 使用 ExchangeUser 对象显示用户名和 SMTP 地址。

下面的托管代码是使用 C# 编写的。 若要运行需要调入组件对象模型 (COM) 的 .NET Framework 托管代码示例,您必须使用可定义托管接口并将其映射到对象模型类型库中的 COM 对象的互操作程序集。 对于 Outlook,您可以使用 Visual Studio 和 Outlook 主互操作程序集 (PIA)。 在您运行适用于 Outlook 2013 的托管代码示例之前,请确保您已安装了 Outlook 2013 PIA 并且已添加了对 Visual Studio 中的 Microsoft Outlook 15.0 对象库组件的引用。

应使用 Office Developer Tools for Visual Studio) 在 Outlook 外接程序 (类中使用以下代码 ThisAddIn 。 代码中的 应用程序对象必须是由 提供的受信任 Outlook ThisAddIn.Globals对象。 有关使用 Outlook PIA 开发托管 Outlook 解决方案的详细信息,请参阅欢迎使用 MSDN 上的 Outlook 主互操作程序集参考

private void EnumerateAccounts() 
{ 
    Outlook.Accounts accounts = 
        Application.Session.Accounts; 
 
    // Enumerate each account defined in the current profile. 
    foreach (Outlook.Account account in accounts) 
    { 
        try 
        { 
            StringBuilder sb = new StringBuilder(); 
            sb.AppendLine("Account: " + account.DisplayName); 
 
            // If the account does not contain the SMTP address or 
            // user name, use the AddressEntry and ExchangeUser objects. 
            if (string.IsNullOrEmpty(account.SmtpAddress) 
                || string.IsNullOrEmpty(account.UserName)) 
            { 
                Outlook.AddressEntry oAE = 
                    account.CurrentUser.AddressEntry 
                    as Outlook.AddressEntry; 
 
                // If the account is an Exchange account, 
                // display also the Exchange server name and version. 
                if (oAE.Type == "EX") 
                { 
                    Outlook.ExchangeUser oEU = 
                        oAE.GetExchangeUser() 
                        as Outlook.ExchangeUser; 
  
                    // Use ExchangeUser object to display user name 
                    // and SMTP address. 
                    sb.AppendLine("UserName: " + 
                        oEU.Name); 
                    sb.AppendLine("SMTP: " + 
                        oEU.PrimarySmtpAddress); 
 
                    // Use Account object to display the Exchange 
                    // server name and version information. 
                    sb.AppendLine("Exchange Server: " + 
                        account.ExchangeMailboxServerName); 
                    sb.AppendLine("Exchange Server Version: " + 
                        account.ExchangeMailboxServerVersion);  
                } 
                // The account is not connected to an Exchange 
                // Server, use the AddressEntry object to display only  
                // the user name and SMTP address. 
                else 
                { 
                    sb.AppendLine("UserName: " + 
                        oAE.Name); 
                    sb.AppendLine("SMTP: " + 
                        oAE.Address); 
                } 
            } 
            // The account contains SMTP address and 
            // user name, then the Account object is sufficient.  
            else 
            { 
                sb.AppendLine("UserName: " + 
                    account.UserName); 
                sb.AppendLine("SMTP: " + 
                    account.SmtpAddress); 
 
                // If the account is an Exchange account, 
                // display also the Exchange server name and version. 
                if(account.AccountType ==  
                    Outlook.OlAccountType.olExchange) 
                { 
                    sb.AppendLine("Exchange Server: " + 
                        account.ExchangeMailboxServerName); 
                    sb.AppendLine("Exchange Server Version: " + 
                        account.ExchangeMailboxServerVersion);  
                } 
            } 
 
            // If the account is connected to a delivery store, 
            // display the store name as well. 
            if(account.DeliveryStore !=null) 
            { 
                sb.AppendLine("Delivery Store: " + 
                    account.DeliveryStore.DisplayName); 
            } 
            sb.AppendLine("---------------------------------"); 
            Debug.Write(sb.ToString()); 
        } 
        catch (Exception ex) 
        { 
            Debug.WriteLine(ex.Message); 
        } 
    } 
} 

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。