Search code examples
c#powershellvisual-studio-2012exchange-server-2010runspace

How do I add multiple email addresses?


Below is the code i'm using. I'm unable to add multiple addresses using the powershell Emailaddresses parameter. The code works fine if I just put in one email address, but once I add two addresses in the code below it returns exception stating invalid smtp address.

PSCredential credential = new PSCredential(username, password);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
runspace.Open();
powershell.Runspace = runspace;

var secure = new SecureString();
foreach (char c in textBox5.Text)
{
    secure.AppendChar(c);
}

PSCommand command2 = new PSCommand();
command2.AddCommand("Set-Mailbox");
command2.AddParameter("Identity", "lferrigno");
command2.AddParameter("EmailAddressPolicyEnabled", 0);
command2.AddParameter("EmailAddresses", "SMTP:[email protected],[email protected]");

powershell.Commands = command2;
powershell.Invoke();

Solution

  • This is the code i ended up using since it was a collection.

         string[] smtp = { "SMTP:" + textBox6.Text, 9 + "smtp:" + textBox4.Text + "@sscincorporated.com" };
         command2.AddParameter("EmailAddresses", smtp);