原文地址:
 
本文主要介绍如何在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;”
并在最后加上:
InBlock.gif[assembly: ApplicationActivation(ActivationOption.Server)]    
InBlock.gif[assembly: ApplicationName( "你的application名字")]    
InBlock.gif[assembly: Description( "PowerShell组件")]    
InBlock.gif[assembly: ApplicationAccessControl(    
InBlock.gif                     false,    
InBlock.gif                     AccessChecksLevel = AccessChecksLevelOption.Application,    
InBlock.gif                     Authentication = AuthenticationOption.None,    
InBlock.gif                     ImpersonationLevel = ImpersonationLevelOption.Identify)]
添加代码
1)将 class1.cs 改为 “ManagementCommands.cs”.
类需要继承System.EnterpriseServices.ServicedComponent
2)代码如下
           
InBlock.gifRunspaceConfiguration config = RunspaceConfiguration.Create();
InBlock.gif                        PSSnapInException warning;
InBlock.gif
InBlock.gif                        config.AddPSSnapIn( "Microsoft.Exchange.Management.PowerShell.Admin", out warning);
InBlock.gif                         if (warning != null) throw warning;
InBlock.gif
InBlock.gif                        Runspace thisRunspace = RunspaceFactory.CreateRunspace(config)
InBlock.gif                        thisRunspace.Open();
InBlock.gif                        Pipeline thisPipeline = thisRunspace.CreatePipeline();
InBlock.gif
InBlock.gif                        thisPipeline.Commands.Add( "Enable-Mailbox");
InBlock.gif                        thisPipeline.Commands[0].Parameters.Add( "Identity", @"DOM157711\xxxxxxx");
InBlock.gif                        thisPipeline.Commands[0].Parameters.Add( "Database", @"XXXXXXX-8\First Storage Group\Mailbox Database");
InBlock.gif                        thisPipeline.Commands[0].Parameters.Add( "DomainController", domainController);
InBlock.gif                        thisPipeline.Invoke();
InBlock.gif                        thisRunspace.Close();
 创建和配置COM+组件
生成工程
用Visual Studio 命令行工具 运行 "regsvcs PowerShellComponent.dll"
然后 在管理工具-->组件服务-->我的电脑-->COM+应用程序-->PowerShellComponent
右击属性-标识,在“下列用户”中输入Exchange的管理员用户名及密码
组件就创建完成
在应用程序中使用

把PowerShellComponent.dll拷贝到.net应用程序的bin目录下,添加引用。

      
InBlock.gifPowerShellComponent.ManagementCommands objManage = new PowerShellComponent.ManagementCommands();
InBlock.gif
InBlock.gif             String Results;
InBlock.gif             Results = objManage.EnableMailbox(domain1, domainController1);
InBlock.gif             Response.Write(Results);