다음을 통해 공유


Deleting attachments by using the EWS Managed API 2.0

You can use the Exchange Web Services (EWS) Managed API to delete attachments from items.

Last modified: February 06, 2014

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

To delete an attachment

  1. Bind to an existing item by using its unique identifier. The following code shows how to bind to an existing message item, requesting its first class properties plus its attachments collection, and provide it with connection configuration information by using an ExchangeService object named service. The ItemId has been shortened to preserve readability.

    EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkA"), new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments)); 
    
  2. Delete the attachment in one of the following ways:

    • By using the RemoveAt method. The RemoveAt method accepts an integer that represents the zero-based index position of the attachment to delete. The following code shows how to delete the first attachment in the attachments collection, provided that the message has at least one attachment.

      if (message.HasAttachments)
      {
         message.Attachments.RemoveAt(0);
      }
      
    • By using the Remove method. The following code shows how to loop through the attachments collection, identify the attachment in the collection by using the attachment name, and delete the attachment from the collection.

      foreach (Attachment attachment in message.Attachments)
      {
          if (attachment.Name.ToUpper() == "THIRDATTACHMENT.JPG")
          {
              message.Attachments.Remove(attachment);
              break;
          }
      }
      
  3. Save the updated item.

    message.Update(ConflictResolutionMode.AlwaysOverwrite);
    

To delete all attachments

  1. Bind to an existing item by using its unique identifier. The following code shows how to bind to an existing message item, requesting its first class properties plus its attachments collection, and provide it with connection configuration information by using an ExchangeService object named service. The ItemId has been shortened to preserve readability.

    EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkA"), new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));
    
  2. Delete all attachments on the message item, as shown in the following code.

    message.Attachments.Clear();
    
  3. Save the updated item.

    message.Update(ConflictResolutionMode.AlwaysOverwrite);
    

Example

The following code example shows how to delete attachments from an existing message. It deletes the second attachment in the attachments collection and also deletes the attachment named "Attachment1.txt." This example assumes that service is a valid ExchangeService binding. The ItemId has been shortened to preserve readability.

// Bind to an existing message by using its unique identifier, requesting its first class properties, plus its attachments collection.
EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkA"), new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));

// If the message contains at least two attachments, delete the second attachment.
if (message.Attachments.Count > 1)
{
   message.Attachments.RemoveAt(1);
}

// Iterate through the attachments collection and delete the attachment named "Attachment1.txt," if it exists.
foreach (Attachment attachment in message.Attachments)
{
    if (attachment.Name.ToUpper() == "ATTACHMENT1.TXT")
    {
        message.Attachments.Remove(attachment);
        break;
    }
}

// Save the updated message.
message.Update(ConflictResolutionMode.AlwaysOverwrite);

Compiling the code

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

Robust programming

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.

  • Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.

Security

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.