我正在尝试在C#中创建一个Exchange邮箱。以下代码不会产生错误,但也不会像我期望的那样创建邮箱:

private void buttonCreateUser_Click(object sender, EventArgs e)
{
    Boolean Success = CreateUser(textBoxFirstName.Text, textBoxLastName.Text,
        textBoxAlias.Text, textBoxPassword.Text,
        comboBoxDomain.SelectedItem.ToString(),
        comboBoxOrganizationalUnit.SelectedItem.ToString());

    if (Success)
    {
        labelStatus.Text = "User Created";
    }
    else
    {
        labelStatus.Text = "There Is Some Error";
    }

}

public Boolean CreateUser(string FirstName, string LastName, string Alias,
    string PassWord, string DomainName, string OrganizationalUnit)
{
    string Name = FirstName + " " + LastName;
    string PrincipalName = FirstName + "." + LastName + "@" + DomainName;

    Boolean success = false;
    RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
    SecureString spassword = new SecureString();
    spassword.Clear();

    foreach (char c in PassWord)
    {
        spassword.AppendChar(c);
    }

    PSSnapInException snapInException = null;
    PSSnapInInfo info = rsConfig.AddPSSnapIn(
        "Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
    Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
    myRunSpace.Open();
    Pipeline pipeLine = myRunSpace.CreatePipeline();

    Command myCommand = new Command("New-MailBox");
    myCommand.Parameters.Add("Name", Name);
    myCommand.Parameters.Add("Alias", Alias);
    myCommand.Parameters.Add("UserPrincipalName", PrincipalName);
    myCommand.Parameters.Add("Confirm", true);
    myCommand.Parameters.Add("SamAccountName", Alias);
    myCommand.Parameters.Add("FirstName", FirstName);
    myCommand.Parameters.Add("LastName", LastName);
    myCommand.Parameters.Add("Password", spassword);
    myCommand.Parameters.Add("ResetPasswordOnNextLogon", false);
    myCommand.Parameters.Add("OrganizationalUnit", OrganizationalUnit);
    pipeLine.Commands.Add(myCommand);
    pipeLine.Invoke();
    myRunSpace.Dispose();

    success = true;

    return success;
}
我没有收到错误,所以我不知道自己在做什么错。
更新
我正在为此使用Web Service。如果我在Windows应用程序中运行相同的代码,则可以运行,但不能与WebService一起运行?我应该在Exchange Server中进行任何更改吗?虽然我可以使用Get-MailBox获取邮箱的信息,但是New-MailBox不能创建用户。

最佳答案

我已经为这个问题苦苦挣扎了几天(本周之前我对C#语言一无所知)。无论如何,对于像我这样的人,希望实现这一目标,但是很努力,这是一个对我有用的示例:

我们使用Exchange2010,我可以从没有安装交换工具的计算机上运行它。

using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Management.Automation.Host;
using System.Collections.ObjectModel;
using Microsoft.PowerShell.Commands;

static string createmailbox(string name, string alias, string email, string database, string UPN)
        {

            SecureString spassword = new SecureString();
            string PassWord = "<set default password here or read in input from form>";
            spassword.Clear();

            foreach (char c in PassWord)
            {
                spassword.AppendChar(c);
            }

            string orgunit = "<define the OU here if you always use the same one, alternatively, add as parameter in function call>";
            string dc = "<similarly, add DC here if needed, or call  from function>";
            PSCredential newCred = (PSCredential)null;
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("<put in CAS server FQDN here>/powershell?serializationLevel=Full"),
                "http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
            Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
            PowerShell powershell = PowerShell.Create();

            PSCommand command = new PSCommand();
            command.AddCommand("New-Mailbox");
            command.AddParameter("-Name", name);
            command.AddParameter("-Alias", alias);
            command.AddParameter("-UserPrincipalName", email);
            command.AddParameter("-PrimarySMTPAddress", email);
            command.AddParameter("-Password",spassword);
            // (ConvertTo-SecureString -AsPlainText "P4ssw0rd" -Force)
            command.AddParameter("-Database", database);
            command.AddParameter("-OrganizationalUnit", orgunit);
 //           command.AddParameter("-Email", email);
            command.AddParameter("-DomainController", dc);
            powershell.Commands = command;
            try
            {
                runspace.Open();
                powershell.Runspace = runspace;
                Collection<PSObject> results = powershell.Invoke();
                return results.ToString();
            }
            catch (Exception ex)
            {
                string er = ex.InnerException.ToString();

            }
            finally
            {
                runspace.Dispose();
                runspace = null;

                powershell.Dispose();
                powershell = null;

            }
        }

我的用例是通过WPF表单,因此参数是从表单上的文本框中填充的。我已经设置了一些密码,例如密码(我们为共享邮箱设置了默认密码,并且禁用了用户帐户),OU和域 Controller 为静态文本,但是可以通过变量轻松地调用它们。

关于c# - 如何创建Exchange邮箱?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10115014/

10-12 14:14