原文地址:
本文主要介绍如何在asp.net中对Exchange服务器进行管理员级别的操作,主要实现是通过Exchange 管理工具实现,因此,程序主机必须安装Exchange 管理工具。
最近的一个项目需求,需要在sharepoint服务器上实现创建AD账号,并启用邮箱。
按照以前的仅有的开发经验,弱智的尝试用Web Service实现,用管理员的Credential,但是Web Service提供的29中operation中只是类似于outlook的操作。又因为,2007版本中没有CDOEXM,只能通过Exchange Management Shell。因为创建邮箱这样的操作必须具有高权限,而在自己的Application中的用户不一定有此权限,所以需要使用COM+实现。
具体方法如下:
1、首先程序机器上必须安装有Windows PowerShell 和 Exchange 管理工具。
2、创建项目,选择类库。
3、添加引用System.EnterpriseServices, System.Management.Automation(C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\)。强命名!
4、在AssemblyInfo.cs中添加 “using System.EnterpriseServices;”
并在最后加上:
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationName(
"你的application名字")]
[assembly: Description(
"PowerShell组件")]
[assembly: ApplicationAccessControl(
false,
AccessChecksLevel = AccessChecksLevelOption.Application,
Authentication = AuthenticationOption.None,
ImpersonationLevel = ImpersonationLevelOption.Identify)]
添加代码
1)将 class1.cs 改为 “ManagementCommands.cs”.
类需要继承System.EnterpriseServices.ServicedComponent
2)代码如下
RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException warning;
config.AddPSSnapIn(
"Microsoft.Exchange.Management.PowerShell.Admin",
out warning);
if (warning !=
null)
throw warning;
Runspace thisRunspace = RunspaceFactory.CreateRunspace(config)
thisRunspace.Open();
Pipeline thisPipeline = thisRunspace.CreatePipeline();
thisPipeline.Commands.Add(
"Enable-Mailbox");
thisPipeline.Commands[0].Parameters.Add(
"Identity",
@"DOM157711\xxxxxxx");
thisPipeline.Commands[0].Parameters.Add(
"Database",
@"XXXXXXX-8\First Storage Group\Mailbox Database");
thisPipeline.Commands[0].Parameters.Add(
"DomainController", domainController);
thisPipeline.Invoke();
thisRunspace.Close();
创建和配置COM+组件
生成工程
用Visual Studio 命令行工具 运行 "regsvcs PowerShellComponent.dll"
然后 在管理工具-->组件服务-->我的电脑-->COM+应用程序-->PowerShellComponent
右击属性-标识,在“下列用户”中输入Exchange的管理员用户名及密码
组件就创建完成
在应用程序中使用
把PowerShellComponent.dll拷贝到.net应用程序的bin目录下,添加引用。
PowerShellComponent.ManagementCommands objManage =
new PowerShellComponent.ManagementCommands();
String Results;
Results = objManage.EnableMailbox(domain1, domainController1);
Response.Write(Results);