Share via


Account Object

Outlook Developer Reference

The Account object represents an account defined for the current profile.

Version Information
 Version Added:  Outlook 2007

Remarks

The purpose of the Accounts collection object and the Account object is to allow the enumeration of Account objects in a given profile, the identification of the type of an Account, and the use of a specific Account object to send mail.

MVP logo Helmut Obertanner provided the following code samples. Helmut is a Microsoft Most Valuable Professional with expertise in Microsoft Visual Studio Tools for Office and Microsoft Office Outlook. Helmut maintains a professional site at http://www.outlooksharp.de/.

Example

The following managed code samples are written in C# and Visual Basic. 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 Microsoft Visual Studio and the Microsoft Office Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2007, ensure that you have installed the Outlook 2007 PIA and have added a reference to the Microsoft Outlook 12.0 Object Library component in Visual Studio. For more information about using the Outlook PIA to develop managed Outlook solutions, see Welcome to the Outlook 2007 Primary Interop Assembly Reference.

The following code samples show the DisplayAccountInformation method of the Sample class, implemented as part of an Outlook add-in project. Each project adds a reference to the Outlook PIA, which is based on the Microsoft.Office.Interop.Outlook namespace. The DisplayAccountInformation method takes as an input argument a trusted Outlook Application object, and uses the Account object to display the details of each account available for the current Outlook profile.

C#
  using System;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookAddIn1 { class Sample { public static void DisplayAccountInformation(Outlook.Application application) {

        // The Namespace Object (Session) has a collection of accounts.
        Outlook.Accounts accounts = application.Session.Accounts;

        // Concatenate a message with information about all accounts.
        StringBuilder builder = new StringBuilder();

        // Loop over all accounts and print detail account information.
        // All properties of the Account object are read-only.
        foreach (Outlook.Account account in accounts)
        {

            // The DisplayName property represents the friendly name of the account.
            builder.AppendFormat("DisplayName: {0}\n", account.DisplayName);

            // The UserName property provides an account-based context to determine identity.
            builder.AppendFormat("UserName: {0}\n", account.UserName);

            // The SmtpAddress property provides the SMTP address for the account.
            builder.AppendFormat("SmtpAddress: {0}\n", account.SmtpAddress);

            // The AccountType property indicates the type of the account.
            builder.Append("AccountType: ");
            switch (account.AccountType)
            {

                case Outlook.OlAccountType.olExchange:
                    builder.AppendLine("Exchange");
                    break;

                case Outlook.OlAccountType.olHttp:
                    builder.AppendLine("Http");
                    break;

                case Outlook.OlAccountType.olImap:
                    builder.AppendLine("Imap");
                    break;

                case Outlook.OlAccountType.olOtherAccount:
                    builder.AppendLine("Other");
                    break;

                case Outlook.OlAccountType.olPop3:
                    builder.AppendLine("Pop3");
                    break;
            }

            builder.AppendLine();
        }

        // Display the account information.
        System.Windows.Forms.MessageBox.Show(builder.ToString());
    }
}

}

Visual Basic
  Imports Outlook = Microsoft.Office.Interop.Outlook

Namespace OutlookAddIn2 Class Sample Shared Sub DisplayAccountInformation(ByVal application As Outlook.Application)

        '	The Namespace Object (Session) has a collection of accounts.
        Dim accounts As Outlook.Accounts = application.Session.Accounts

        ' Concatenate a message with information about all accounts.
        Dim builder As StringBuilder = New StringBuilder()

        ' Loop over all accounts and print detail account information.
        ' All properties of the Account object are read-only.
        Dim account As Outlook.Account
        For Each account In accounts

            ' The DisplayName property represents the friendly name of the account.
            builder.AppendFormat("DisplayName: {0}" & vbNewLine, account.DisplayName)

            ' The UserName property provides an account-based context to determine identity.
            builder.AppendFormat("UserName: {0}" & vbNewLine, account.UserName)

            ' The SmtpAddress property provides the SMTP address for the account.
            builder.AppendFormat("SmtpAddress: {0}" & vbNewLine, account.SmtpAddress)

            ' The AccountType property indicates the type of the account.
            builder.Append("AccountType: ")
            Select Case (account.AccountType)

                Case Outlook.OlAccountType.olExchange
                    builder.AppendLine("Exchange")


                Case Outlook.OlAccountType.olHttp
                    builder.AppendLine("Http")


                Case Outlook.OlAccountType.olImap
                    builder.AppendLine("Imap")


                Case Outlook.OlAccountType.olOtherAccount
                    builder.AppendLine("Other")


                Case Outlook.OlAccountType.olPop3
                    builder.AppendLine("Pop3")


            End Select

            builder.AppendLine()
        Next


        ' Display the account information.
        Windows.Forms.MessageBox.Show(builder.ToString())
    End Sub


End Class

End Namespace

See Also