共用方式為


取得多個帳戶的資訊

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 地址。

The following managed code is written in C#. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio.

您應該使用 Office Developer Tools for Visual Studio) ,在 Outlook 增益集 (類別中使用下列程式碼 ThisAddIn 。 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱 MSDN 上的歡迎使用 Outlook 主要 Interop 元件參考

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 支援與意見反應