获取帐户信息
本主题将输入参数用作受信任 Outlook Application 对象,并使用 Account 对象来显示可用于当前 Outlook 配置文件的每个帐户的详细信息。
示例
注意
Helmut Obertanner 提供了下面的代码示例。 Helmut 拥有 Visual Studio Office 开发人员工具和 Outlook 方面的专业知识。
下面的代码示例包含 Sample 类的 DisplayAccountInformation 方法(作为 Outlook 加载项项目的一部分实现)。 每个项目都添加对基于 Microsoft.Office.Interop.Outlook 命名空间的 Outlook 主互操作程序集的引用。
如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 Imports 或 using 语句直接添加到此代码示例中的函数前面,这两个语句必须后跟公共类声明。 下面的代码行演示了如何在 Visual Basic 和 C# 中执行导入和分配。
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
下面是 Visual Basic 代码示例,后跟 C# 代码示例。
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
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());
}
}
}