如何创建交换邮箱
本文关键字:交换 创建 何创建 | 更新日期: 2023-09-27 18:31:00
我正在尝试用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;
}
我没有收到错误,所以我不知道我做错了什么。
更新
我正在使用网络服务为此。如果我在Windows应用程序上运行相同的代码,它可以工作,但不能使用WebService?我应该在 Exchange Server 中进行任何更改吗?虽然我可以使用Get-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 和域控制器等内容设置为静态文本,但它们可以很容易地通过变量调用。
我得到了这个的解决方案.我更改了代理的权限.dll呜它的工作很棒......