使用EWS托管API创建共享邮箱

时间:2017-11-19 22:58:45

标签: c# .net exchangewebservices

如何连接到Exchange服务器并创建共享邮箱。我找不到使用AWS Managed API的方法。有什么建议?感谢。

1 个答案:

答案 0 :(得分:0)

好吧,我找到了这样做的方法,如果有人需要它,这就是解决方案(从控制台应用程序执行cmdlet):

public class Service
{
    public static ExchangeService GetExchangeService(string username, string password, string ExchangeUrl)
    {
        var exchangeService = new ExchangeService(ExchangeVersion.Exchange2013);

        //WebService Uri
        try
        {
            exchangeService.Url = new Uri(ExchangeUrl);
        }
        catch (Exception ex)
        {
            throw new Exception(string.Format("WebService Uri:" + ex));
        }

        //Credentials
        try
        {
            exchangeService.Credentials = new WebCredentials(username, password);
        }
        catch (Exception ex)
        {
            throw new Exception(string.Format("Credentials:" + ex));
        }
        return exchangeService;
    }
}

 static void Main(string[] args)
    {
        ExchangeService service = null;
        service = Service.GetExchangeService("email@myexchangedomain.com", "mypassword", "https://outlook.office365.com/EWS/Exchange.asmx");

      var emailCreateCommand = CreateEmailCommand("name", "displayName", "alias");
      var res2 = ExecuteCommand(emailCreateCommand);

     }


private static Command CreateEmailCommand(string name, string displayName, string alias)
    {
        Command myCommand = new Command("New-MailBox");
        myCommand.Parameters.Add("Shared", true);
        myCommand.Parameters.Add("Name", name);
        myCommand.Parameters.Add("DisplayName", displayName);
        myCommand.Parameters.Add("Alias", alias);

        return myCommand;
    }



public static Collection<PSObject> ExecuteCommand(Command command)
    {
        string pass = "mypassword";
        System.Security.SecureString securePassword = new System.Security.SecureString();

        foreach (char c in pass.ToCharArray())
        {
            securePassword.AppendChar(c);
        }

        PSCredential newCred = new PSCredential("email@myexchangedomain.com", securePassword);


        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
            new Uri("https://outlook.office365.com/PowerShell-LiveID"),
            "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
            newCred);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

        Runspace myRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
        myRunSpace.Open();

        Pipeline pipeLine = myRunSpace.CreatePipeline();

        pipeLine.Commands.Add(command);
        Collection<PSObject> result = pipeLine.Invoke();
        myRunSpace.Close();
        return result;
    }
相关问题