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

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

      
在asp.net中使用Powershell操作Exchange 2007(启用邮箱)_休闲PowerShellComponent.ManagementCommands objManage = new PowerShellComponent.ManagementCommands();
在asp.net中使用Powershell操作Exchange 2007(启用邮箱)_休闲
在asp.net中使用Powershell操作Exchange 2007(启用邮箱)_休闲             String Results;
在asp.net中使用Powershell操作Exchange 2007(启用邮箱)_休闲             Results = objManage.EnableMailbox(domain1, domainController1);
在asp.net中使用Powershell操作Exchange 2007(启用邮箱)_休闲             Response.Write(Results);