在 Exchange 中使用 EWS 预配 x 标头

了解如何使用 Exchange 中的 EWS 托管 API 或 EWS 为邮箱预配 x 标头。

X 标头是添加到电子邮件标头集合以传达信息的非标准标头。 例如,Exchange 标记带有 X-MS-Exchange-Organization-SCL 标头的邮件,以指示 (SCL) 归因于电子邮件的垃圾邮件置信度级别。 例如,Email Outlook 等客户端可以使用该信息来确定要对电子邮件 (执行的操作类型,除非用户) 执行操作,否则 Outlook 可以阻止显示图像。

Exchange 在首次收到带有该 x 标头的电子邮件时,将传入的 x 标头作为命名属性添加到邮箱架构。 x 标头值不会保存在第一封电子邮件上;但是,它会保存在包含 x 标头的所有后续电子邮件上。 因此,应用程序应在预期使用 x 标头之前预配它们。 命名属性和 x 标头之间的映射发生在电子邮件到邮箱的传输传递中。 这意味着你需要通过传输传递接收电子邮件;不能只将包含 x 标头的电子邮件保存到邮箱,以创建到命名属性的映射。

注意

如果发现 x 标头未保存,请在到达邮箱之前确定 传输代理标头防火墙 是否正在筛选掉你的 x 标头。 r

使用 EWS 托管 API 预配 x 标头

下面的代码示例演示如何使用 EWS 托管 API EmailMessage.Send 方法为邮箱预配 x 标头。 此示例假定 服务 是有效的 ExchangeService 对象,并且目标邮箱未超过 命名属性的配额

private static void ProvisionCustomXHeaderByEmail(ExchangeService service)
{
    // Create a definition for an extended property that will represent a custom x-header. X-headers must be created in the
    // InternetHeaders property set. The x-header name must match the name of the x-header sent in the subsequent emails so
    // the x-header and value are saved on the email.
    ExtendedPropertyDefinition xExperimentalHeader = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.InternetHeaders,
                                                                                            "X-Experimental",
                                                                                            MapiPropertyType.String);
    // Create an item that is used to provision the custom x-header.
    EmailMessage email = new EmailMessage(service);
    email.ToRecipients.Add("user@contoso.com");
    email.SetExtendedProperty(xExperimentalHeader, "Provision X-Experimental Internet message header");
    try
    {
        // The mapping of the named property happens in transport delivery.
        email.Send();
        if (service.ServerInfo.MajorVersion == 12)
        {
            Console.WriteLine("Provisioned the X-Experimental across the mailbox database that hosts the user's mailbox.");
        }
        else // For versions of Exchange starting with Exchange 2010
        {
            Console.WriteLine("Provisioned the X-Experimental for the user's mailbox. You will need to run this " +
                                "for each mailbox that needs to process this x-header.");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: {0}", ex.Message);
    }
}

使用 EWS 预配 x 标头

下面的代码示例演示如何使用 EWS CreateItem 操作创建和发送电子邮件来预配具有 x 标头的邮箱。 这是在使用 EWS 托管 API 预配 x 标头时由 EWS 托管 API 发送的 XML 请求。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
               xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2013" />
  </soap:Header>
  <soap:Body>
    <m:CreateItem MessageDisposition="SendOnly">
      <m:Items>
        <t:Message>
          <t:ExtendedProperty>
            <t:ExtendedFieldURI DistinguishedPropertySetId="InternetHeaders"
                                PropertyName="X-Experimental"
                                PropertyType="String" />
            <t:Value>Provision X-Experimental Internet message header</t:Value>
          </t:ExtendedProperty>
          <t:ToRecipients>
            <t:Mailbox>
              <t:EmailAddress>user@contoso.com</t:EmailAddress>
            </t:Mailbox>
          </t:ToRecipients>
        </t:Message>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

版本差异

第一次在 Exchange Online、Exchange Online 作为Office 365的一部分或从 Exchange Server 2010 开始的 Exchange 本地版本预配 x 标头时,新的自定义 x 标头的值将不会写入存储的邮件。 这是因为 x 标头必须首先映射到用户邮箱中的命名属性。 映射发生在添加命名属性的第一个请求时。 当后续请求创建命名属性时,属性和值存储在消息中。 在 Exchange 2007 中,第一次将 x 标头写入邮箱数据库时,会为 x 标头创建跨邮箱数据库的命名属性的映射。 当后续请求创建命名属性时,将针对 Exchange 2007 数据库中的任何邮箱处理和存储 x 标头。

后续步骤

本文介绍如何通过向用户发送电子邮件来为单个邮箱预配 x 标头。 还可以通过向调用方组织中的 收件人列表发送批电子邮件 ,为许多用户预配 x 标头。

另请参阅