Jump to content

Creating Exchange MailBox C# Help

CGameDev

I coded an application to create Active Directory Users and wanted to add a section that adds their Exchange mailbox. I keep getting the following error. I have tried doing some search on how to resolve it, none of the solution work so far.

Is there an alternate way to add a mailbox or is there a known solution to this issue, Please!

image.png.990dcc24e33e022282431f50806f29e1.png

 

I am using the following code

 public Boolean CreateUserMailbox(string FirstName, string LastName, string Alias, string PassWord, string DomainName, string OrganizationalUnit)
        {
            string Name = FirstName + " " + LastName;
            string PrincipalName = Alias + "@" + 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();
            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", false);
            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.Commands.Add(myCommand);
            try
            {
                pipeLine.Invoke();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myRunSpace.Dispose();
                success = true;
            }
            return success;
        }

 

Link to comment
Share on other sites

Link to post
Share on other sites

A quick Google suggests you get this error, because the machine you're running the application on does not have the Exchange 2010 Management Tools installed. Hence the Snap-In isn't available for PowerShell. Did you install the management tools?

 

8 hours ago, CGameDev said:

I have tried doing some search on how to resolve it, none of the solution work so far.

Would help if you listed what you've already tried. Otherwise people are simply going to suggest the same things again.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

As @Eigenvektorsaid that is one issue. If you still have a problem and if you are connected to that ^#$%^ office 365 in your domain then you need to wait for the new user to be pushed to the cloud service as well before assigning extra properties. I never found any methods exist to force push that from the API side but within Exchange UI itself you can do it manually.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×