.net开通exchange邮箱

阅读:985 2019-03-20 14:09:18 来源:新网

最近在工作中负责解决用代码在exchange中为用户创建邮箱的问题

主要就是用c#封装一个exchange操作类,在exchange的服务器上发布一个webservice供系统的java代码调用

t01144d31d02dd7ebfb.jpg

在查阅很多资料后,终于在

http://www.cnblogs.com/xiaogelove/archive/2011/02/17/1956617.html

这篇帖子的指导下把这个问题解决了

不过这个帖子讲得并不是特别的详细

所以我觉得有必要把我在这个过程中遇到的问题记录下来

所以在上面这篇帖子的基础上写了一个相对详细的新流程

==============================================================================

主要原理是:(这个原理是我猜的,因为我也不太懂exchange以及com组件这方面的知识)

直接用c#代码访问exchange是不行的

微软出了一个powershell的命令行工具能够用命令行来操作exchange

可以通过把.net类注册成com+组件的方式来操作powershell

所以我的流程就是

webservice->.net写的powershell操作类注册成的com+组件->exchange

环境是:

vs2010+exchange2010+windowsserver200864位版+iis7.0

ps:这个com+组件只能运行在安装exchange的服务器上

公司的环境用的是exchange2010,exchange2010好像只有64位版的只能安装在64位的系统上

所以下面会说到把com+组件编译成64位的问题

==============================================================================

1首先先创建com组件并注册

1)启动visualstudio2010

2)选择file->“新建->”项目...

3)选择windows

4)选择“类库”

5)在名称框中键入“powershellcomponent“

6)点击确定。

7)添加下列引用

system.enterpriseservices

system.directoryservices

system.management.automation路径:

32位系统:

c:programfilesreferenceassembliesmicrosoftwindowspowershellv1.system.management.automation.dll

64位系统

c:programfiles(x86)referenceassembliesmicrosoftwindowspowershellv1.0system.management.automation.dll

接下来有关程序集的操作

1)在解决方案资源管理器,右键单击powershellcomponent项目,选择属性,点击签名选项,选中"为程序集签名",并创建一个新的强名称密钥称为“powershellcomponent.snk”,不用设置密码。如下图

2)还是在项目属性窗口中,选择"应用程序"选项卡,然后点击“程序集信息...”,检查框,选中"使程序集com可见"。如图

ps:如果运行这个com组件的机器是64位系统(32位的没试过),这里需要再加一步:

把项目的运行平台设置成64位的

还是在项目属性窗口中:

"生成"选项卡->目标平台->64位

->

3)打开assemblyinfo.cs中,并添加“usingsystem.enterpriseservices;”,并添加

[assembly:applicationactivation(activationoption.server)]

[assembly:applicationname("powershellcomponent")]

[assembly:description("simplepowershellcomponentsample")]

[assembly:applicationaccesscontrol(

false,

accesscheckslevel=accesschecksleveloption.application,

authentication=authenticationoption.none,

impersonationlevel=impersonationleveloption.identify)]

然后添加managementcommands类...

1)选择“解决方案资源管理器”查看选项卡。将class1.cs文件重命名为“managementcommands.cs”。

类需要继承system.enterpriseservices.servicedcomponent,否则不能被编译成com+组件

2)添加引用如图并using

usingsystem.enterpriseservices;

usingsystem.security;

usingsystem.security.principal;

usingsystem.runtime.interopservices;

usingsystem.collections.objectmodel;

usingsystem.management.automation;

usingsystem.management.automation.host;

usingsystem.management.automation.runspaces;

usingsystem.directoryservices;

usingmicrosoft.powershell.commands;

usingsystem.collections;

3)拷贝下面的方法到类中

复制代码

1#region根据登录名判断是否存在邮箱23publicboolisexistmailbox(stringidentity)45{67try89{1011pssnapinexceptionpsexception=null;1213runspaceconfigurationrunspaceconf=runspaceconfiguration.create();1415runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);1617runspacerunspace=runspacefactory.createrunspace(runspaceconf);1819runspace.open();20212223pipelinepipeline=runspace.createpipeline();2425commandcommand=newcommand("get-mailbox");2627command.parameters.add("identity",identity);2829pipeline.commands.add(command);3031collectionresult=pipeline.invoke();32333435runspace.close();36373839return(result!=null&&result.count>0);4041}4243catch(system.exceptionex)4445{4647throwex;4849}5051}5253#endregion54555657#region创建邮箱账号5859publicboolnewmailbox(stringname,stringaccountname,stringpwd,stringemaildomain,stringorganizationalunit,stringdatabase)6061{6263stringemailadd=accountname+emaildomain;64656667if(this.isexistmailbox(emailadd))6869{7071thrownewexception("已经存在同名的邮箱");7273}7475try7677{7879pssnapinexceptionpsexception=null;8081runspaceconfigurationrunspaceconf=runspaceconfiguration.create();8283runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);8485runspacerunspace=runspacefactory.createrunspace(runspaceconf);8687runspace.open();8889pipelinepipeline=runspace.createpipeline();90919293commandcommand=newcommand("new-mailbox");9495char[]passwordchars=pwd.tochararray();9697securestringpassword=newsecurestring();9899foreach(charcinpasswordchars)100101{102103password.appendchar(c);104105}106107108109command.parameters.add("name",name);//姓名110111112113command.parameters.add("userprincipalname",emailadd);//邮箱地址114115command.parameters.add("samaccountname",accountname);//登录名116117118119command.parameters.add("password",password);//密码120121122123command.parameters.add("organizationalunit",organizationalunit);//组织单元124125command.parameters.add("database",database);//数据库126127128129pipeline.commands.add(command);130131collectionresult=pipeline.invoke();132133runspace.close();134135136137returnthis.isexistmailbox(emailadd);138139}140141catch(exceptionex)142143{144145throwex;146147}148149}150151#endregion152153154155#region删除邮箱账号(控制台和域都删除)156157158159publicboolremovemailbox(stringidentity)160161{162163164165try166167{168169pssnapinexceptionpsexception=null;170171runspaceconfigurationrunspaceconf=runspaceconfiguration.create();172173runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);174175runspacerunspace=runspacefactory.createrunspace(runspaceconf);176177runspace.open();178179pipelinepipeline=runspace.createpipeline();180181182183commandcommand=newcommand("remove-mailbox");184185command.parameters.add("identity",identity);186187command.parameters.add("confirm",false);188189pipeline.commands.add(command);190191collectionresult=pipeline.invoke();192193runspace.close();194195196197return!this.isexistmailbox(identity);198199}200201catch(system.exceptionex)202203{204205throwex;206207}208209}210211#endregion212213214215#region启用邮箱账号216217publicboolenablemailbox(stringidentity)218219{220221try222223{224225pssnapinexceptionpsexception=null;226227runspaceconfigurationrunspaceconf=runspaceconfiguration.create();228229runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);230231runspacerunspace=runspacefactory.createrunspace(runspaceconf);232233runspace.open();234235pipelinepipeline=runspace.createpipeline();236237238239commandcommand=newcommand("enable-mailbox");240241command.parameters.add("identity",identity);242243command.parameters.add("confirm",false);244245pipeline.commands.add(command);246247collectionresult=pipeline.invoke();248249runspace.close();250251returnthis.isexistmailbox(identity);252253}254255catch(exceptionex)256257{258259throwex;260261}262263}264265#endregion266267268269#region禁用邮箱账号270271publicbooldisablemailbox(stringidentity)272273{274275try276277{278279pssnapinexceptionpsexception=null;280281runspaceconfigurationrunspaceconf=runspaceconfiguration.create();282283runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);284285runspacerunspace=runspacefactory.createrunspace(runspaceconf);286287runspace.open();288289290291pipelinepipeline=runspace.createpipeline();292293commandcommand=newcommand("disable-mailbox");294295command.parameters.add("identity",identity);296297command.parameters.add("confirm",false);298299pipeline.commands.add(command);300301collectionresult=pipeline.invoke();302303runspace.close();304305return!this.isexistmailbox(identity);306307}308309catch(exceptionex)310311{312313throwex;314315}316317}318319#endregion320321322323#region判断是否存在通讯组324325publicboolisexistgroup(stringidentity)326327{328329try330331{332333pssnapinexceptionpsexception=null;334335runspaceconfigurationrunspaceconf=runspaceconfiguration.create();336337runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);338339runspacerunspace=runspacefactory.createrunspace(runspaceconf);340341runspace.open();342343344345pipelinepipeline=runspace.createpipeline();346347commandcommand=newcommand("get-distributiongroup");348349command.parameters.add("identity",identity);350351pipeline.commands.add(command);352353collectionresult=pipeline.invoke();354355356357runspace.close();358359360361return(result!=null&&result.count>0);362363}364365catch(system.exceptionex)366367{368369throwex;370371}372373}374375#endregion376377378379#region创建通讯组380381publicboolnewgroup(stringname)382383{384385if(this.isexistgroup(name))386387{388389thrownewexception("已经存在相同的通讯组");390391}392393try394395{396397pssnapinexceptionpsexception=null;398399runspaceconfigurationrunspaceconf=runspaceconfiguration.create();400401runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);402403runspacerunspace=runspacefactory.createrunspace(runspaceconf);404405runspace.open();406407408409pipelinepipeline=runspace.createpipeline();410411commandcommand=newcommand("new-distributiongroup");412413command.parameters.add("name",name);414415pipeline.commands.add(command);416417collectionresult=pipeline.invoke();418419runspace.close();420421returnthis.isexistgroup(name);422423}424425catch(exceptionex)426427{428429throwex;430431}432433}434435436437#endregion438439440441#region删除通讯组442443publicboolremovegroup(stringidentity)444445{446447try448449{450451pssnapinexceptionpsexception=null;452453runspaceconfigurationrunspaceconf=runspaceconfiguration.create();454455runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);456457runspacerunspace=runspacefactory.createrunspace(runspaceconf);458459runspace.open();460461462463pipelinepipeline=runspace.createpipeline();464465commandcommand=newcommand("remove-distributiongroup");466467command.parameters.add("identity",identity);468469command.parameters.add("confirm",false);470471pipeline.commands.add(command);472473collectionresult=pipeline.invoke();474475runspace.close();476477return!this.isexistgroup(identity);478479}480481catch(exceptionex)482483{484485throwex;486487}488489}490491#endregion492493494495#region添加通讯组成员496497publicbooladdgroupmember(stringgroupidentity,stringmailidentity)498499{500501try502503{504505pssnapinexceptionpsexception=null;506507runspaceconfigurationrunspaceconf=runspaceconfiguration.create();508509runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);510511runspacerunspace=runspacefactory.createrunspace(runspaceconf);512513runspace.open();514515516517pipelinepipeline=runspace.createpipeline();518519commandcommand=newcommand("add-distributiongroupmember");520521command.parameters.add("identity",groupidentity);522523command.parameters.add("member",mailidentity);524525pipeline.commands.add(command);526527collectionresult=pipeline.invoke();528529runspace.close();530531returntrue;532533}534535catch(exceptionex)536537{538539throwex;540541}542543}544545#endregion546547548549#region删除通讯组成员550551publicboolremovegroupmember(stringgroupidentity,stringmailidentity)552553{554555try556557{558559pssnapinexceptionpsexception=null;560561runspaceconfigurationrunspaceconf=runspaceconfiguration.create();562563runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);564565runspacerunspace=runspacefactory.createrunspace(runspaceconf);566567runspace.open();568569570571pipelinepipeline=runspace.createpipeline();572573commandcommand=newcommand("remove-distributiongroupmember");574575command.parameters.add("identity",groupidentity);576577command.parameters.add("member",mailidentity);578579command.parameters.add("confirm",false);580581pipeline.commands.add(command);582583collectionresult=pipeline.invoke();584585runspace.close();586587returntrue;588589}590591catch(exceptionex)592593{594595throwex;596597}598599}600601#endregion602603

复制代码

ps:这些都是exchange的命令,暂时只封装了这么多,如果想实现更多的功能,只需要照着上面的例子把实现相应的exchange命令就行了

在微软的官网上有exchange的命令文档http://msdn.microsoft.com/zh-cn/library/aa997174.aspx

最后运行生成项目,得到powershellcomponent.dll,com组件就创建好了。

接下来就是注册这个组件了:

步骤一:

【控制面板】→【管理工具】→【组件服务】

步骤二:

出现窗口后,【组件服务】→【计算机】→【我的电脑】→【com+应用程序】单击右键→新建→应用程序→安装向导下一步→创建空应用程序→输入空应用程序名称:powershellcomponent,并选择激活类型为服务器应用程序→设置应用程序标示(账号选择下列用户账号和密码是该服务器登录用户名和密码)→完成。

右键单击创建出来的powershellcomoponent,选择属性,找到"标志"选项卡,选择”下列用户“填入计算机的登录用户名和密码,确定

步骤三:

创建好应用程序后打开powershellcomponent出现【组件】【旧版组件】【角色】在【组件】上单击右键→新建→组件

步骤三:

点下一步,出现如下窗口,选择【安装新组件】:

选择前面项目生成的powershellcomponent.dll文件→【打开】点下一步,选择完成。

步骤四:

为刚刚注册的powershellcomponent组件添加用户权限

打开powershellcomponent下面的【角色】-【creatorowner】-【用户】右键【新建】-【用户】

在出来的窗口点[高级]-[位置]-选择[整个目录]-[立即查找]

因为webservicce是发布在iis上面的所以我的iis需要有权限来操作这个com组件所以我添加的是iis的用户

在搜索出来的结果里面选择iis_iusrs并添加,如果是用winform来调用这个com+组件则应该要添加管理员帐号administrator

用户添加完了组件就注册成功了。

把powershellcomponent.dll拷到测试项目中

测试项目添加对powershellcomponent.dll的引用就能被调用了

如果你的com+组件被启用了在调试过程中如果你需要重新编译你的dll文件那么需要先关闭com+组件dll才能被重新编译

如果在调用的过程中发生异常,可能是两个方面的原因:

1如果com+组件在64位的环境下运行是否有被编译成64位

2权限问题

还有一个

因为操作的是exchange2010所以代码中是

pssnapininfoinfo=runspaceconf.addpssnapin("microsoft.exchange.management.powershell.e2010",outpsexception);

如果你的exchange是2007的那么这行代码可能需要被改成

pssnapininfoinfo=runspaceconf.addpssnapin("microsoft.exchange.management.powershell.admin",outpsexception);

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服