1

我正在尝试在 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 服务。如果我使用 Windows 应用程序运行相同的代码,它可以工作,但不能使用 WebService?我应该在 Exchange Server 中进行任何更改吗?虽然我可以使用 Get-MailBox 获取 MailBox 的信息,但 New-MailBox 不能创建用户。

4

2 回答 2

2

几天来,我一直在为这个问题苦苦挣扎(在这周之前,我对 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 和域控制器等设置为静态文本,但它们也可以通过变量轻松调用。

于 2016-01-20T18:33:34.277 回答
1

I get the solution for this .I change permission leval of inproxy.dll and whooooo its working great ...

于 2012-04-13T20:37:09.070 回答