none
Add an additional email address to an existing mailbox RRS feed

  • Question

  • Hello,

    I have a requirement to add an additional email address to all my users. There are many users and for this reason I would like to do this pragmatically (Visual Studio, C#, .NET).

    I kindly request some guidance, sample code to get me started.

    Many thanks

    DK

    Monday, August 3, 2015 12:49 PM

All replies

  • You need to use the Exchange Management Shell cmdlet which you can execute via Remote PowerShell from you managed code https://msdn.microsoft.com/en-us/library/office/ff326159%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396 . To modify the ProxyAddresses of a Mailbox you need to get the existing entries modify that list and then update the Mailbox using Set-Mailbox eg

    string PSServerName = "ps.outlook.com/PowerShell-LiveID?PSVersion=2.0";
    String AdminUserName = "user@domain.onmicrosoft.com";
    String Password = "password";
    
    System.Security.SecureString secureString = new System.Security.SecureString();
    foreach (char c in Password)
        secureString.AppendChar(c);
    PSCredential credential = new PSCredential(AdminUserName, secureString);
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://" + PSServerName), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
    connectionInfo.SkipCACheck = true;
    connectionInfo.SkipCNCheck = true;
    
    connectionInfo.MaximumConnectionRedirectionCount = 4;
    Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
    runspace.Open();
    Pipeline plPileLine = runspace.CreatePipeline();
    Command gmGetMailbox = new Command("Get-Mailbox");
    gmGetMailbox.Parameters.Add("Identity", "user@domain.onmicrosoft.com");
    plPileLine.Commands.Add(gmGetMailbox);
    Collection<PSObject> RsResultsresults = plPileLine.Invoke();
    if (plPileLine.Error.Count == 1)
    {
    
        var error = plPileLine.Error.Read() as Collection<ErrorRecord>;
        if (error != null)
        {
            foreach (ErrorRecord er in error)
            {
                  Console.WriteLine(er.Exception.Message);
            }
        }
    
    }
    plPileLine.Stop();
    foreach (PSObject obj in RsResultsresults)
    {
        PSObject prProxyAddress = (PSObject)obj.Properties["EmailAddresses"].Value;
        if (prProxyAddress.ImmediateBaseObject is System.Collections.ArrayList)
        {
            ArrayList Addresses = (System.Collections.ArrayList)prProxyAddress.BaseObject;
            Addresses.Add("smtp:newaddr@domain.onmicrosoft.com");
            plPileLine = runspace.CreatePipeline();
            Command smSetMailbox  = new Command("Set-Mailbox");
            smSetMailbox.Parameters.Add("Identity", "user@domain.onmicrosoft.com");
            smSetMailbox.Parameters.Add("EmailAddresses", Addresses);
            plPileLine.Commands.Add(smSetMailbox);
            RsResultsresults = plPileLine.Invoke();
            if (plPileLine.Error.Count == 1)
            {
    
                var error = plPileLine.Error.Read() as Collection<ErrorRecord>;
                if (error != null)
                {
                    foreach (ErrorRecord er in error)
                    {
                        Console.WriteLine(er.Exception.Message);
                    }
                }
    
            }
        }
    
    }          
    plPileLine.Dispose();
    runspace.Close();
    runspace.Dispose();
    runspace = null;
    plPileLine = null;
    Cheers
    Glen

    Tuesday, August 4, 2015 4:10 AM