【问题标题】:How to create an Exchange mailbox?如何创建 Exchange 邮箱?
【发布时间】:2012-04-11 22:43:29
【问题描述】:

我正在尝试用 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 没有创建用户。

【问题讨论】:

  • 您正在构建的 POSH 命令是否符合您的预期?
  • @M.Babcock 用户未添加到邮箱中
  • 在尝试使用使问题复杂化的 API 动态构建之前,手动计算出 POSH 命令的详细信息。
  • 我用 POSH 命令尝试了这个,没有任何问题,用户被添加但不是以编程方式
  • 你应该传入一个安全的字符串。从普通字符串构建安全字符串没有什么意义(这就是为什么没有构造函数来做到这一点)

标签: c# powershell exchange-server-2010


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我得到了解决方案。我更改了 inproxy.dll 的权限级别,哇哦,它工作得很好......

    【讨论】:

      猜你喜欢
      • 2013-11-09
      • 2012-03-29
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多