How to create an Exchange mailbox?
-
30-05-2021 - |
質問
I am trying to create an Exchange mailbox in C#. The following code doesn't produce an error, but it also doesn't appear to create a mailbox as I would expect:
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;
}
I don't get an error, so I don't know what I am doing wrong.
Update
I am using Web Service For this.If I run same code with windows application it works,But not with WebService? should I make any change in Exchange Server? Though I can Get information of MailBox with Get-MailBox but New-MailBox not Creating User.
解決 2
I get the solution for this .I change permission leval of inproxy.dll and whooooo its working great ...
他のヒント
I've been struggling with this very issue for a few days (I knew nothing about C# as a language before this week). Anyway, for anyone like me, looking to implement this, but struggling, here is an example that works for me:
We use Exchange2010, and I can run this from a machine that doesn't have the exchange tools installed.
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;
}
}
My use case is via a WPF form, so the parameters are populated from text boxes on the form. I've set things like the password (we have a default password for shared mailboxes and the user account is disabled), the OU and the domain controller as static text, but they could be called via variables just as easily.