我正在尝试在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而不是创建用户。
答案 0 :(得分: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和域控制器作为静态文本,但可以通过变量调用它们。
答案 1 :(得分:1)
我为此获得了解决方案。我更改了inproxy.dll的权限级别,并且whoooo工作得很好...