使用 Exchange 中的 EWS 在阻止的发件人列表中添加和删除电子邮件地址Add and remove email addresses from the Blocked Senders List by using EWS in Exchange

了解如何使用 EWS 托管 API 或 EWS 将电子邮件地址添加到阻止的发件人列表并将其从该列表中删除。Find out how to use the EWS Managed API or EWS to add email addresses to and remove them from the Blocked Senders List.

用户的 "垃圾邮件" 选项中的 "阻止发件人" 列表提供了将所有电子邮件从指定发件人移动到 "垃圾邮件" 文件夹的方法。The Blocked Senders List in a user's Junk Email options provides a way to move all email messages from specified senders to the Junk Email folder. 您可以启用您的 EWS 托管 API 或 EWS 应用程序,将电子邮件地址添加到阻止的发件人列表或从中删除它们。You can enable your EWS Managed API or EWS application to add email addresses to or remove them from the Blocked Senders List.

请注意,来自电子邮件地址的邮件必须存在于用户的邮箱中,然后才能将电子邮件地址添加到阻止发件人列表或从阻止发件人列表中删除。Note that a message from the email address must exist in the user's mailbox before you can add the email address to or remove it from the Blocked Senders List. ExchangeService EWS 托管 API 方法和MarkAsJunk EWS 操作使用项目 id 的集合。The ExchangeService.MarkAsJunk EWS Managed API method and the MarkAsJunk EWS operation use a collection of item IDs. 集合中的项目 Id 指示邮箱中应更改其垃圾邮件状态的邮件。The item IDs in the collection indicate messages in the mailbox for which the junk mail status should be changed.

您可以使用set-mailboxjunkemailconfiguration和 Set-mailboxjunkemailconfiguration Exchange 命令行管理程序cmdlet 直接访问阻止的发件人列表。You can use the Get-MailboxJunkEmailConfiguration and Set-MailboxJunkEmailConfiguration Exchange Management Shell cmdlets to access the Blocked Senders List directly.

使用 EWS 托管 API 将电子邮件地址添加到阻止发件人列表或从阻止的发件人列表中删除Add an email address to or remove it from the Blocked Senders List by using the EWS Managed API

若要将电子邮件的发件人添加到阻止的发件人列表中,请使用MarkAsJunk方法并将isJunk参数设置为trueTo add the sender of an email message to the Blocked Senders List, use the MarkAsJunk method and set the isJunk parameter to true. 若要从阻止的发件人列表中删除电子邮件的发件人,请将isJunk参数设置为falseTo remove the sender of an email message from the Blocked Senders List, set the isJunk parameter to false.

下面的示例演示如何使用MarkAsJunk方法更改邮件的垃圾邮件状态。The following example shows how to use the MarkAsJunk method to change the junk status of a message.

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 将电子邮件地址添加到阻止发件人列表或从阻止的发件人列表中删除Add an email address to or remove it from the Blocked Senders List by using EWS

下面的 EWS SOAP 请求通过将MarkAsJunk元素上的IsJunk属性设置为true,将项标记为垃圾。The following EWS SOAP request marks an item as junk by setting the IsJunk attribute on the MarkAsJunk element to true. 它还通过将MarkAsJunk元素上的MoveItem属性设置为true,将邮件移动到 "垃圾邮件" 文件夹。It also moves the message to the Junk Email folder by setting the MoveItem attribute on the MarkAsJunk element to true.

<?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 响应显示成功的响应。The following EWS SOAP response shows the successful response. 响应中的MovedItemId元素包含项目在移动后的项目 ID。The MovedItemId element in the response contains the item ID for the item after it was moved.

<?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>

另请参阅See also