.net开通exchange邮箱

发布时间:2023-10-27 点击:131
最近在工作中负责解决 用代码在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 windows server 2008 64位版 iis7.0
ps:这个com 组件只能运行在安装exchange的服务器上
公司的环境用的是exchange2010, exchange2010好像只有64位版的 只能安装在64位的系统上
所以下面会说到把com 组件编译成64位的问题
==============================================================================
1 首先先创建com组件并注册
1)启动visual studio 2010
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:program files (x86)reference assembliesmicrosoftwindowspowershellv1.0system.management.automation.dll
接下来有关程序集的操作
1)在解决方案资源管理器,右键单击powershellcomponent项目,选择属性,点击签名选项,选中”为程序集签名”,并创建一个新的强名称密钥称为“powershellcomponent.snk”,不用设置密码。如下图
2)还是在项目属性窗口中,选择”应用程序”选项卡,然后点击“程序集信息…”,检查框,选中”使程序集com可见”。如图
ps:如果运行这个com组件的机器是64位系统(32位的没试过),这里需要再加一步:
把项目的运行平台设置成64位的
还是在项目属性窗口中:
“生成”选项卡->目标平台->64位
->
3)打开assemblyinfo.cs中,并添加“using system.enterpriseservices;”,并添加
[assembly: applicationactivation(activationoption.server)]
[assembly: applicationname(“powershellcomponent”)]
[assembly: description(“simple powershell component sample”)]
[assembly: applicationaccesscontrol(
false,
accesscheckslevel = accesschecksleveloption.application,
authentication = authenticationoption.none,
impersonationlevel = impersonationleveloption.identify)]
然后添加managementcommands类…
1)选择“解决方案资源管理器”查看选项卡。将class1.cs文件重命名为“managementcommands.cs”。
类需要继承system.enterpriseservices.servicedcomponent,否则不能被编译成com 组件
2)添加引用如图并using
using system.enterpriseservices;
using system.security;
using system.security.principal;
using system.runtime.interopservices;
using system.collections.objectmodel;
using system.management.automation;
using system.management.automation.host;
using system.management.automation.runspaces;
using system.directoryservices;
using microsoft.powershell.commands;
using system.collections;
3)拷贝下面的方法到类中
复制代码
1 #region 根据登录名判断是否存在邮箱 2 3 public bool isexistmailbox(string identity) 4 5 { 6 7 try 8 9 { 10 11 pssnapinexception psexception = null; 12 13 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 14 15 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 16 17 runspace runspace = runspacefactory.createrunspace(runspaceconf); 18 19 runspace.open(); 20 21 22 23 pipeline pipeline = runspace.createpipeline(); 24 25 command command = new command(“get-mailbox”); 26 27 command.parameters.add(“identity”, identity); 28 29 pipeline.commands.add(command); 30 31 collection result = pipeline.invoke(); 32 33 34 35 runspace.close(); 36 37 38 39 return (result != null && result.count > 0); 40 41 } 42 43 catch (system.exception ex) 44 45 { 46 47 throw ex; 48 49 } 50 51 } 52 53 #endregion 54 55 56 57 #region 创建邮箱账号 58 59 public bool newmailbox(string name, string accountname, string pwd, string emaildomain, string organizationalunit, string database) 60 61 { 62 63 string emailadd = accountname emaildomain; 64 65 66 67 if (this.isexistmailbox(emailadd)) 68 69 { 70 71 throw new exception(“已经存在同名的邮箱”); 72 73 } 74 75 try 76 77 { 78 79 pssnapinexception psexception = null; 80 81 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 82 83 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 84 85 runspace runspace = runspacefactory.createrunspace(runspaceconf); 86 87 runspace.open(); 88 89 pipeline pipeline = runspace.createpipeline(); 90 91 92 93 command command = new command(“new-mailbox”); 94 95 char[] passwordchars = pwd.tochararray(); 96 97 securestring password = new securestring(); 98 99 foreach (char c in passwordchars) 100 101 { 102 103 password.appendchar(c); 104 105 } 106 107 108 109 command.parameters.add(“name”, name);//姓名 110 111 112 113 command.parameters.add(“userprincipalname”, emailadd);//邮箱地址 114 115 command.parameters.add(“samaccountname”, accountname);//登录名 116 117 118 119 command.parameters.add(“password”, password);//密码 120 121 122 123 command.parameters.add(“organizationalunit”, organizationalunit);//组织单元 124 125 command.parameters.add(“database”, database);//数据库 126 127 128 129 pipeline.commands.add(command); 130 131 collection result = pipeline.invoke(); 132 133 runspace.close(); 134 135 136 137 return this.isexistmailbox(emailadd); 138 139 } 140 141 catch (exception ex) 142 143 { 144 145 throw ex; 146 147 } 148 149 } 150 151 #endregion 152 153 154 155 #region 删除邮箱账号(控制台和域都删除) 156 157 158 159 public bool removemailbox(string identity) 160 161 { 162 163 164 165 try 166 167 { 168 169 pssnapinexception psexception = null; 170 171 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 172 173 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 174 175 runspace runspace = runspacefactory.createrunspace(runspaceconf); 176 177 runspace.open(); 178 179 pipeline pipeline = runspace.createpipeline(); 180 181 182 183 command command = new command(“remove-mailbox”); 184 185 command.parameters.add(“identity”, identity); 186 187 command.parameters.add(“confirm”, false); 188 189 pipeline.commands.add(command); 190 191 collection result = pipeline.invoke(); 192 193 runspace.close(); 194 195 196 197 return !this.isexistmailbox(identity); 198 199 } 200 201 catch (system.exception ex) 202 203 { 204 205 throw ex; 206 207 } 208 209 } 210 211 #endregion 212 213 214 215 #region 启用邮箱账号 216 217 public bool enablemailbox(string identity) 218 219 { 220 221 try 222 223 { 224 225 pssnapinexception psexception = null; 226 227 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 228 229 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 230 231 runspace runspace = runspacefactory.createrunspace(runspaceconf); 232 233 runspace.open(); 234 235 pipeline pipeline = runspace.createpipeline(); 236 237 238 239 command command = new command(“enable-mailbox”); 240 241 command.parameters.add(“identity”, identity); 242 243 command.parameters.add(“confirm”, false); 244 245 pipeline.commands.add(command); 246 247 collection result = pipeline.invoke(); 248 249 runspace.close(); 250 251 return this.isexistmailbox(identity); 252 253 } 254 255 catch (exception ex) 256 257 { 258 259 throw ex; 260 261 } 262 263 } 264 265 #endregion 266 267 268 269 #region 禁用邮箱账号 270 271 public bool disablemailbox(string identity) 272 273 { 274 275 try 276 277 { 278 279 pssnapinexception psexception = null; 280 281 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 282 283 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 284 285 runspace runspace = runspacefactory.createrunspace(runspaceconf); 286 287 runspace.open(); 288 289 290 291 pipeline pipeline = runspace.createpipeline(); 292 293 command command = new command(“disable-mailbox”); 294 295 command.parameters.add(“identity”, identity); 296 297 command.parameters.add(“confirm”, false); 298 299 pipeline.commands.add(command); 300 301 collection result = pipeline.invoke(); 302 303 runspace.close(); 304 305 return !this.isexistmailbox(identity); 306 307 } 308 309 catch (exception ex) 310 311 { 312 313 throw ex; 314 315 } 316 317 } 318 319 #endregion 320 321 322 323 #region 判断是否存在通讯组 324 325 public bool isexistgroup(string identity) 326 327 { 328 329 try 330 331 { 332 333 pssnapinexception psexception = null; 334 335 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 336 337 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 338 339 runspace runspace = runspacefactory.createrunspace(runspaceconf); 340 341 runspace.open(); 342 343 344 345 pipeline pipeline = runspace.createpipeline(); 346 347 command command = new command(“get-distributiongroup”); 348 349 command.parameters.add(“identity”, identity); 350 351 pipeline.commands.add(command); 352 353 collection result = pipeline.invoke(); 354 355 356 357 runspace.close(); 358 359 360 361 return (result != null && result.count > 0); 362 363 } 364 365 catch (system.exception ex) 366 367 { 368 369 throw ex; 370 371 } 372 373 } 374 375 #endregion 376 377 378 379 #region 创建通讯组 380 381 public bool newgroup(string name) 382 383 { 384 385 if (this.isexistgroup(name)) 386 387 { 388 389 throw new exception(“已经存在相同的通讯组”); 390 391 } 392 393 try 394 395 { 396 397 pssnapinexception psexception = null; 398 399 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 400 401 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 402 403 runspace runspace = runspacefactory.createrunspace(runspaceconf); 404 405 runspace.open(); 406 407 408 409 pipeline pipeline = runspace.createpipeline(); 410 411 command command = new command(“new-distributiongroup”); 412 413 command.parameters.add(“name”, name); 414 415 pipeline.commands.add(command); 416 417 collection result = pipeline.invoke(); 418 419 runspace.close(); 420 421 return this.isexistgroup(name); 422 423 } 424 425 catch (exception ex) 426 427 { 428 429 throw ex; 430 431 } 432 433 } 434 435 436 437 #endregion 438 439 440 441 #region 删除通讯组 442 443 public bool removegroup(string identity) 444 445 { 446 447 try 448 449 { 450 451 pssnapinexception psexception = null; 452 453 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 454 455 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 456 457 runspace runspace = runspacefactory.createrunspace(runspaceconf); 458 459 runspace.open(); 460 461 462 463 pipeline pipeline = runspace.createpipeline(); 464 465 command command = new command(“remove-distributiongroup”); 466 467 command.parameters.add(“identity”, identity); 468 469 command.parameters.add(“confirm”, false); 470 471 pipeline.commands.add(command); 472 473 collection result = pipeline.invoke(); 474 475 runspace.close(); 476 477 return !this.isexistgroup(identity); 478 479 } 480 481 catch (exception ex) 482 483 { 484 485 throw ex; 486 487 } 488 489 } 490 491 #endregion 492 493 494 495 #region 添加通讯组成员 496 497 public bool addgroupmember(string groupidentity, string mailidentity) 498 499 { 500 501 try 502 503 { 504 505 pssnapinexception psexception = null; 506 507 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 508 509 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 510 511 runspace runspace = runspacefactory.createrunspace(runspaceconf); 512 513 runspace.open(); 514 515 516 517 pipeline pipeline = runspace.createpipeline(); 518 519 command command = new command(“add-distributiongroupmember”); 520 521 command.parameters.add(“identity”, groupidentity); 522 523 command.parameters.add(“member”, mailidentity); 524 525 pipeline.commands.add(command); 526 527 collection result = pipeline.invoke(); 528 529 runspace.close(); 530 531 return true; 532 533 } 534 535 catch (exception ex) 536 537 { 538 539 throw ex; 540 541 } 542 543 } 544 545 #endregion 546 547 548 549 #region 删除通讯组成员 550 551 public bool removegroupmember(string groupidentity, string mailidentity) 552 553 { 554 555 try 556 557 { 558 559 pssnapinexception psexception = null; 560 561 runspaceconfiguration runspaceconf = runspaceconfiguration.create(); 562 563 runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception); 564 565 runspace runspace = runspacefactory.createrunspace(runspaceconf); 566 567 runspace.open(); 568 569 570 571 pipeline pipeline = runspace.createpipeline(); 572 573 command command = new command(“remove-distributiongroupmember”); 574 575 command.parameters.add(“identity”, groupidentity); 576 577 command.parameters.add(“member”, mailidentity); 578 579 command.parameters.add(“confirm”, false); 580 581 pipeline.commands.add(command); 582 583 collection result = pipeline.invoke(); 584 585 runspace.close(); 586 587 return true; 588 589 } 590 591 catch (exception ex) 592 593 { 594 595 throw ex; 596 597 } 598 599 } 600 601 #endregion 602 603
复制代码
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 所以代码中是
pssnapininfo info = runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.e2010”, out psexception);
如果你的exchange是2007的 那么这行代码可能需要被改成
pssnapininfo info = runspaceconf.addpssnapin(“microsoft.exchange.management.powershell.admin”, out psexception);
如果,你对上面的内容还有疑问,推荐选择西部数码企业云邮箱!有专人协助您解答邮箱疑问。
西部数码是专业企业邮箱的官方正规提供商,21年行业经验,提供安全稳定,简单易用,高性价比的企业邮箱,按需自由定制,不限空间,极速收发,能够满足用户对企业邮箱的不同需求。可以通过以下几种方式注册、申请、购买、试用、开通企业邮箱:
1、登录https://www.west.cn/services/mail/在线咨询申请试用或购买;
2、直接致电028-62778877申请试用或正式购买开通;


南京云主机服务器租用费用
百度云服务器重启
免备案香港云服务器秒杀
电脑下载chrome浏览器上不了网的原因及解决方案
云计算有什么用途
公司邮箱怎么设置邮件提示
昭通市云服务器租用
华云数据亮相“三体一云”大会 助力金蝶用户无缝实现云端转型