【问题标题】:How do I make mail contacts in Microsoft Exchange?如何在 Microsoft Exchange 中建立邮件联系人?
【发布时间】:2019-09-26 13:42:23
【问题描述】:
我正在寻找另一种使用 C# 在 Microsoft Exchange 中创建邮件联系人的方法,通常使用 Microsoft Exchange 2016 的 EWS API,而不是依赖 Powershell 命令New-MailContact。我知道 SO 上有 a very similar question,但它已于 2012 年发布,因此希望在这两者之间出现一些新内容。
如果不是这种情况,是否有任何方法可以在 Active Directory 中创建一个联系人,该联系人会在 Exchange 中反映为邮件联系人?
【问题讨论】:
标签:
c#
exchange-server
exchangewebservices
【解决方案1】:
您可能需要使用Exchange Management Shell。代码看起来类似于这个 sn-p:
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;
}