[C#][WinForm]Create Mail for Exchange2007

  • 5774
  • 0
  • C#
  • 2009-12-07

[C#][WinForm]Create Mail for Exchange2007

剛好最近公司打算申級Mail Server、AD,所以幫MIS減輕一下負擔,別讓建立郵件信箱成為最大夢靨,本想應該很快就搞定,但事情往往不是我所想的><,沒想到Exchange2007改了API,取而代之便是Powershell,哀~本想拿以前的程式(for Exchange2003)改改,看來是行不通了。anyway~先利用exchange管理工具透過UI介面手動建立MAIL完成後出現以下指令:

Enable-Mailbox -Identity 'your domain/your cn/user account' -Alias 'your alias' -Database 'your server name\First Storage Group\MailBox Database'

Feel大哥來了,使用C#調用powershell執行上面指令應該就可以了,

嘗試了兩天終於搞定。不過感覺C#調用powershell和直接執行cmdlet有所不同(小弟對powershell不熟悉><)。

1.先加入powershell參考(C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll)

2.本機需安裝exchange2007管理工具(32bit/64bit),因過程中需調用exchange管理工具API執行

小弟列出自訂義class部分code

using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;

public static string CreateMail(string ldapPath, string adminname, string adminpw, string server, string uname, string upw, string email) 
        { 
            string success = "";
            //確認AD帳號為管理員          
              ........   

            //確認AD使用者是否存在

            ......

            if (SearchResult == null) 
            { 
                return "確認帳號是否存在";                  
            } 
            else 
            {                 

                //判斷使用者mail欄位是否有值

                if (SearchResult.Properties["mail"].ToString==string.Empty) 
                { 
                    return "該帳號信箱已存在"; 
                } 
                else 
                { 
                    UserP1 = SearchResult.Path; 
                    //'預設CN=Users  如果你沒有指定OU的話
                    UserP2 = UserP1.Substring(UserP1.IndexOf("CN"),UserP1.Length - UserP1.IndexOf("CN")); 
                    RunspaceConfiguration runConf = RunspaceConfiguration.Create(); 
                    PSSnapInException pssException = null; 
                    PSSnapInInfo info =  runConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out pssException); 
                    Runspace runsp = RunspaceFactory.CreateRunspace(runConf); 
                    runsp.Open(); 
                    Pipeline pipeline = runsp.CreatePipeline(); 
                    Command command = new Command("Enable-Mailbox"); //powershell指令
                    command.Parameters.Add("Identity", UserP2); 
                    command.Parameters.Add("Alias", uname); 
                    //ExChange資料庫 
                    command.Parameters.Add("Database", server+@"\First Storage Group\Mailbox Database"); 
                    pipeline.Commands.Add(command); 
                    success=pipeline.Invoke().Count.ToString(); 
                    runsp.Close(); 
                    if (entry != null) 
                    { 
                        entry.Close(); 
                    } 
                    if (success == "1") 
                    { 
                        return uname+"信箱建立成功"; 
                    } 
                    else
                       return uname + "信箱建立失敗"; 
                }            
            } 
          return ""; 
        }