使用 Exchange 中的 EWS 删除附件Delete attachments by using EWS in Exchange

了解如何通过使用 Exchange 中的 EWS 托管 API 或 EWS 从项目中删除附件。Learn how to delete attachments from items by using the EWS Managed API or EWS in Exchange.

在使用 EWS 托管 API 从项目中删除文件和项目附件时,您有多种选项。You have a number of options when it comes to deleting file and item attachments from items by using the EWS Managed API. 您可以从邮件中删除所有附件、按文件名删除或在集合中按位置删除。You can delete all the attachments from the message, delete by a file name, or delete by position in the collection. 对于上述每个选项,都有一个AttachmentCollection方法。For each of these options, there is an AttachmentCollection method.

与 EWS 相反,无论您是从项目还是仅从一个项目删除所有附件,操作顺序都是相同的。Conversely, with EWS, no matter whether you're deleting all attachments from an item or just one, the sequence of operations is same. 与 EWS 托管 API 不同,EWS 不包含根据附件数组中的名称或位置来删除的独立操作。Unlike the EWS Managed API, EWS does not include separate operations to delete based on name or position in the attachments array.

表1。用于删除附件的 EWS 托管 API 方法和 EWS 操作Table 1. EWS Managed API methods and EWS operations for deleting attachments

任务Task EWS 托管的 API 方法EWS Managed API method EWS 操作EWS operation
删除项目中的所有附件。Delete all attachments from an item.
项. 先绑定,然后AttachmentCollection,然后EmailMessage。Item.Bind, followed by AttachmentCollection.Clear, followed by EmailMessage.Update
GetItem后接DeleteAttachmentGetItem followed by DeleteAttachment
按名称从项目中删除附件。Delete an attachment from an item by name.
绑定项,然后是AttachmentCollection,然后是EmailMessageItem.Bind, followed by AttachmentCollection.Remove, followed by EmailMessage.Update
GetItem后接DeleteAttachmentGetItem followed by DeleteAttachment
根据集合中的位置从项目中删除附件。Delete an attachment from an item by position in the collection.
项。AttachmentCollection,后跟RemoveAt,后跟EmailMessage. UpdateItem.Bind, followed by AttachmentCollection.RemoveAt, followed by EmailMessage.Update
GetItem后接DeleteAttachmentGetItem followed by DeleteAttachment

使用 EWS 托管 API 删除电子邮件中的所有附件Delete all attachments from an email by using the EWS Managed API

下面的代码示例演示如何通过以下方式删除电子邮件中的所有附件:The following code example shows how to delete all attachments from an email by:

  1. 使用EmailMessage方法可绑定到现有电子邮件并检索附件的集合。Using the EmailMessage.Bind method to bind to an existing email message and retrieve the collection of Attachments.

  2. 使用AttachmentCollection方法可删除电子邮件中的所有附件。Using the AttachmentCollection.Clear method to delete all the attachments from the email.

  3. 使用EmailMessage方法保存所做的更改。Using the EmailMessage.Update method to save the changes.

此示例假定service是有效的ExchangeService对象, itemId是将从中删除附件的邮件的itemid ,并且用户已通过 Exchange 服务器的身份验证。This example assumes that service is a valid ExchangeService object, itemId is the ItemId of the message from which attachments will be deleted, and that the user has been authenticated to an Exchange server.

public static void DeleteAllAttachments(ExchangeService service, ItemId itemId)
{
    // Bind to an existing message by using its item ID and requesting its attachments collection.
    // This method results in a GetItem call to EWS.
    EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
    // Delete all attachments from the message.
    message.Attachments.Clear();
    // Save the updated message.
    // This method results in an DeleteAttachment call to EWS.
    message.Update(ConflictResolutionMode.AlwaysOverwrite);
}

使用 EWS 托管 API 从电子邮件中删除附件Delete an attachment by name from an email by using the EWS Managed API

下面的代码示例演示如何通过以下方式按名称删除附件:The following code example shows how delete an attachment by name by:

  1. 使用EmailMessage方法可绑定到现有电子邮件并检索附件的集合。Using the EmailMessage.Bind method to bind to an existing email message and retrieve the collection of Attachments.

  2. 使用AttachmentCollection方法可删除名为 FileAttachment 的附件。Using the AttachmentCollection.Remove method to delete an attachment named FileAttachment.txt.

  3. 使用EmailMessage方法保存所做的更改。Using the EmailMessage.Update method to save the changes.

此示例假定service是有效的ExchangeService对象, itemId是将从中删除附件的邮件的itemid ,并且用户已通过 Exchange 服务器的身份验证。This example assumes that service is a valid ExchangeService object, itemId is the ItemId of the message from which the attachment will be deleted, and that the user has been authenticated to an Exchange server.

public static void DeleteNamedAttachments(ExchangeService service, ItemId itemId)
{
    // Bind to an existing message by using its item ID and requesting its attachments collection.
    // This method results in a GetItem call to EWS.
    EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
    // Iterate through the attachments collection and delete the attachment named "FileAttachment.txt," if it exists.
    foreach (Attachment attachment in message.Attachments)
    {
        if (attachment.Name == "FileAttachment.txt")
        {
            message.Attachments.Remove(attachment);
            break;
        }
    }
    // Save the updated message.
    // This method results in an DeleteAttachment call to EWS.
    message.Update(ConflictResolutionMode.AlwaysOverwrite);
}

使用 EWS 托管 API 按位置删除附件Delete attachments by position by using the EWS Managed API

下面的代码示例演示如何按位置删除附件:The following code example shows how to delete an attachment by position by:

  1. 使用EmailMessage方法可绑定到现有电子邮件,并检索附件EmailMessage属性的集合。Using the EmailMessage.Bind method to bind to an existing email message and retrieve the collection of Attachments and the EmailMessage.HasAttachments property.

  2. 使用AttachmentCollection方法可删除集合中的第一个附件。Using the AttachmentCollection.Remove method to delete the first attachment in the collection.

  3. 使用EmailMessage方法保存所做的更改。Using the EmailMessage.Update method to save the changes.

此示例假定service是有效的ExchangeService对象, itemId是将从中删除附件的邮件的itemid ,并且用户已通过 Exchange 服务器的身份验证。This example assumes that service is a valid ExchangeService object, itemId is the ItemId of the message from which the attachment will be deleted, and that the user has been authenticated to an Exchange server.

public static void DeleteAttachmentByPosition(ExchangeService service, ItemId itemId)
{
    // Bind to an existing message by using its item ID and requesting the HasAttachments property and the attachments collection.
    // This method results in a GetItem call to EWS.
    EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(EmailMessageSchema.HasAttachments, ItemSchema.Attachments));
    // Remove attachments using the zero-based index position of the attachment in the attachments collection.
    if (message.HasAttachments)
    {
        message.Attachments.RemoveAt(0);
    }
    // Save the updated message.
    // This method results in an DeleteAttachment call to EWS.
    message.Update(ConflictResolutionMode.AlwaysOverwrite);
}

使用 EWS 从项目中删除附件Delete attachments from an item by using EWS

若要使用 EWS 删除附件,首先需要检索邮件和附件集合,以确定要删除的附件的AttachmentId (GetAttachment 和 DeleteAttachment)To delete attachments by using EWS, you first need to retrieve the message and the attachment collection to determine the AttachmentId (GetAttachment and DeleteAttachment) of the attachment to delete. 在具有一个或多个要删除的AttachmentId值后,请调用DeleteAttachment操作以从邮件中删除指定附件。After you have one or more AttachmentId values to delete, call the DeleteAttachment operation to remove the specified attachments from the message.

下面的代码示例演示如何使用GetItem操作来获取电子邮件和邮件附件的集合。The following code example shows how to use the GetItem operation to get an email message and the collection of attachments on the message. 这也是在使用 EWS 托管 API删除电子邮件中的所有附件时,EWS 托管 api 发送的第一个 XML 请求。This is also the first XML request that the EWS Managed API sends when you use the EWS Managed API to delete all attachments from an email. 为了提高可读性,某些属性的值会缩短。The values of some attributes are shortened for readability.

<?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="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Central Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:GetItem>
      <m:ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
        <t:AdditionalProperties>
          <t:FieldURI FieldURI="item:Attachments" />
        </t:AdditionalProperties>
      </m:ItemShape>
      <m:ItemIds>
        <t:ItemId Id="uqE1AAA=" />
      </m:ItemIds>
    </m:GetItem>
  </soap:Body>
</soap:Envelope>

服务器使用GetItemResponse邮件响应GetItem请求,其中包括 ResponseCode 值为NoErrorResponseCode值,该值指示电子邮件已成功检索,以及现有附件的AttachmentId值。The server responds to the GetItem request with a GetItemResponse message that includes a ResponseCode value of NoError, which indicates that the email was retrieved successfully, and the AttachmentId values of the existing attachments.

<?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="939"
                         MinorBuildNumber="12"
                         Version="V2_11"
                         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:GetItemResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
                       xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:GetItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items>
            <t:Message>
              <t:ItemId Id="uqE1AAA="
                        ChangeKey="CQAAABYAAAAFI5DJmZv+TLtyLOLIF1S5AAAXulcd" />
              <t:Attachments>
                <t:FileAttachment>
                  <t:AttachmentId Id="IpHLObE=" />
                  <t:Name>FileAttachment.txt</t:Name>
                </t:FileAttachment>
                <t:FileAttachment>
                  <t:AttachmentId Id="QuHSSmY=" />
                  <t:Name>SecondAttachment.txt</t:Name>
                </t:FileAttachment>
                <t:FileAttachment>
                  <t:AttachmentId Id="qf2KoPo=" />
                  <t:Name>ThirdAttachment.jpg</t:Name>
                </t:FileAttachment>
                <t:FileAttachment>
                  <t:AttachmentId Id="NFQMnMc=" />
                  <t:Name>FourthAttachment.txt</t:Name>
                </t:FileAttachment>
                <t:ItemAttachment>
                  <t:AttachmentId Id="jJvbLXQ=" />
                  <t:Name>Attached Message Item</t:Name>
                </t:ItemAttachment>
              </t:Attachments>
            </t:Message>
          </m:Items>
        </m:GetItemResponseMessage>
      </m:ResponseMessages>
    </m:GetItemResponse>
  </s:Body>
</s:Envelope>

在确定要删除的附件后,请调用DeleteAttachment操作,并包含要删除的附件的AttachmentId值。After you determine which attachment to delete, call the DeleteAttachment operation and include the AttachmentId values of the attachments to delete.

<?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="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Central Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:DeleteAttachment>
      <m:AttachmentIds>
        <t:AttachmentId Id="IpHLObE=" />
        <t:AttachmentId Id="QuHSSmY=" />
        <t:AttachmentId Id="qf2KoPo=" />
        <t:AttachmentId Id="NFQMnMc=" />
        <t:AttachmentId Id="jJvbLXQ=" />
      </m:AttachmentIds>
    </m:DeleteAttachment>
  </soap:Body>
</soap:Envelope>

服务器使用DeleteAttachmentResponse邮件响应DeleteAttachment请求,其中包含每个DeleteAttachmentResponseMessageResponseCodeNoError ,这表明已成功删除每个附件。 为了提高可读性,某些属性的值会缩短。The values of some attributes are shortened for readability.

<?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="939"
                         MinorBuildNumber="12"
                         Version="V2_11"
                         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:DeleteAttachmentResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
                                xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
        <m:DeleteAttachmentResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:RootItemId RootItemId="uqE1AAA=" RootItemChangeKey="AAAXulck" />
        </m:DeleteAttachmentResponseMessage>
      </m:ResponseMessages>
    </m:DeleteAttachmentResponse>
  </s:Body>
</s:Envelope>

另请参阅See also