如何在 Microsoft Exchange 中创建邮件联系人?

How do I make mail contacts in Microsoft Exchange?

我正在寻找使用 C# 在 Microsoft Exchange 中创建邮件联系人的替代方法,通常使用 Microsoft Exchange 2016 的 EWS API 而不是依赖 Powershell 命令 New-MailContact .我知道 SO 上有 a very similar question,但它已于 2012 年发布,所以希望中间出现一些新的东西。

如果不是这种情况,是否有任何方法可以在 Active Directory 中创建一个联系人,该联系人将在 Exchange 中反映为邮件联系人?

您可能需要使用 Exchange Management Shell。该代码将类似于此代码段:

using System;
using System.Security;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

public bool CreateMailContact(string firstName, string lastName, string alias, string email)
    {
      string fullName = firstName + " " + lastName;
      string address = "SMTP:" + ExternalEmail;

      RunspaceConfiguration runspacesConfig = RunspaceConfiguration.Create();

      PSSnapInException snapInException = null;
      PSSnapInInfo info = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.SnapIn", out snapInException);
      Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfig);
      runspace.Open();
      Pipeline pipeline = runspace.CreatePipeline();

      Command createCommand = new Command("New-MailContact");
      createCommand.Parameters.Add("ExternalEmailAddress", address);
      createCommand.Parameters.Add("Name", name);
      createCommand.Parameters.Add("Alias", alias);
      createCommand.Parameters.Add("FirstName", firstName);
      createCommand.Parameters.Add("LastName", lastName);
      pipeline.Commands.Add(createCommand);
      try
      {
        pipeline.Invoke();
      }
      catch (Exception ex)
      {
        Console.WriteLine("An error occurred.")
      }
      finally
      {
        runspace.Dispose();
        return true;
      }
      return false;
    }