在 Exchange 中使用 EWS 从阻止的发件人列表中添加和删除电子邮件地址

了解如何使用 EWS 托管 API 或 EWS 向“阻止发件人列表”添加电子邮件地址并将其从中删除。

用户的“垃圾邮件Email”选项中的“阻止发件人列表”提供了一种将所有电子邮件从指定发件人移动到“垃圾邮件Email”文件夹的方法。 可以启用 EWS 托管 API 或 EWS 应用程序,以便向“阻止发件人列表”添加电子邮件地址或将其从中删除。

请注意,电子邮件地址中的邮件必须存在于用户的邮箱中,然后才能向“阻止发件人列表”添加或从中删除该电子邮件地址。 ExchangeService.MarkAsJunk EWS 托管 API 方法和 MarkAsJunk EWS 操作使用项 ID 的集合。 集合中的项目 ID 指示邮箱中应更改其垃圾邮件状态的邮件。

可以使用 Get-MailboxJunkEmailConfigurationSet-MailboxJunkEmailConfiguration Exchange 命令行管理程序 cmdlet 直接访问阻止的发件人列表。

使用 EWS 托管 API 向阻止发件人列表添加电子邮件地址或将其从中删除

若要将电子邮件的发件人添加到阻止的发件人列表,请使用 MarkAsJunk 方法并将 isJunk 参数设置为 true。 若要从阻止的发件人列表中删除电子邮件的发件人,请将 isJunk 参数设置为 false

以下示例演示如何使用 MarkAsJunk 方法更改邮件的垃圾邮件状态。

private static void MarkMessageAsJunk(ExchangeService service, ItemId messageId, bool isJunk, bool moveItem)
{
    List<ItemId> junkItemIds = new List<ItemId>();
    junkItemIds.Add(messageId);
    ServiceResponseCollection<MarkAsJunkResponse> responseCollection = null;
    try
    {
        // If isJunk is true, the sender of the email message is added to 
        // the Blocked Senders List. If isJunk is false, the sender is removed
        // from the list (if present).
        responseCollection = service.MarkAsJunk(junkItemIds, isJunk, moveItem);
    }
    catch (ServiceResponseException ex)
    {
        Console.WriteLine("Error marking item as junk: {0}", ex.ErrorCode);
        return;
    }
    foreach (MarkAsJunkResponse response in responseCollection)
    {
        if (response.Result == ServiceResult.Success)
        {
            Console.WriteLine("Successfully marked message as {0}junk.", isJunk ? "": "NOT ");
            if (moveItem)
            {
                Console.WriteLine("New item ID: {0}", response.MovedItemId.ToString());
            }
        }
        else
        {
            Console.WriteLine("[{0}]: {1}", response.Result.ToString(),
                response.ErrorCode.ToString());
        }
    }
}

使用 EWS 向“阻止发件人列表”添加电子邮件地址或将其从中删除

以下 EWS SOAP 请求通过将 MarkAsJunk 元素上的 IsJunk 属性设置为 true 将项目标记为垃圾邮件。 它还通过将 MarkAsJunk 元素上的 MoveItem 属性设置为 true,将邮件移动到“垃圾邮件Email”文件夹。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="https://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:MarkAsJunk IsJunk="true" MoveItem="true">
      <m:ItemIds>
        <t:ItemId Id="AAMkADg1OWUwODcyLTg4M2MtNDAyMS05YjI0LTI5ZGM5OTU4Njk3YwBGAAAAAADPriAxh444TpHj2GoQxWQNBwAN+VjmVZl5Rq1ymCq5eFKOAAAAAAENAAAN+VjmVZl5Rq1ymCq5eFKOAAAAAAEuAAA=" 
            ChangeKey="CQAAABYAAAAN+VjmVZl5Rq1ymCq5eFKOAAAAAADi" />
      </m:ItemIds>
    </m:MarkAsJunk>
  </soap:Body>
</soap:Envelope>

以下 EWS SOAP 响应显示成功的响应。 响应中的 MovedItemId 元素包含移动后该项的项目 ID。

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="712" MinorBuildNumber="22" Version="V2_3" 
        xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns="https://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:MarkAsJunkResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
        xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:MarkAsJunkResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:MovedItemId Id="AAMkADg1OWUwODcyLTg4M2MtNDAyMS05YjI0LTI5ZGM5OTU4Njk3YwBGAAAAAADPriAxh444TpHj2GoQxWQNBwAN+VjmVZl5Rq1ymCq5eFKOAAAAAAEbAAAN+VjmVZl5Rq1ymCq5eFKOAAAE59DIAAA="
              ChangeKey="CQAAABYAAAAN+VjmVZl5Rq1ymCq5eFKOAAAE59E+" />
        </m:MarkAsJunkResponseMessage>
      </m:ResponseMessages>
    </m:MarkAsJunkResponse>
  </s:Body>
</s:Envelope>

另请参阅