Share via


Getting attachments by using the EWS Managed API 2.0

You can use the Exchange Web Services (EWS) Managed API to get file and item 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 get a file 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 Id property 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.IdOnly, ItemSchema.Attachments));
    
  2. Retrieve the file attachment in one of the following ways:

    • By loading the attachment into memory. The following code shows how to get the first attachment in the attachments collection, provided that the message has at least one attachment and the first attachment is a file attachment, load it into memory, and print the name of the file attachment.

      if (message.HasAttachments && message.Attachments[0] is FileAttachment)
      { 
         FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
         fileAttachment.Load();
         Console.WriteLine("FileName: " + fileAttachment.FileName);
      }
      
    • By loading the attachment contents into a file. The following code shows how to get the first attachment in the attachments collection, provided that the message has at least one attachment and the first attachment is a file attachment, and load the attachment contents into a file located in the C:\temp directory. If a file by the same name already exists in that location, it will be overwritten with the attachment contents.

      if (message.HasAttachments && message.Attachments[0] is FileAttachment)
      { 
         FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
         fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
      }
      
    • By loading the attachment contents into a Stream. The following code shows how to get the first attachment in the attachments collection, provided that the message has at least one attachment and the first attachment is a file attachment, and load the attachment contents into a Stream.

      if (message.HasAttachments && message.Attachments[0] is FileAttachment)
      { 
         // In this example, theStream is a Stream object into which the attachment contents is loaded.
         FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
         fileAttachment.Load(theStream);
      }
      

To get an item 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 Id property 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.IdOnly, ItemSchema.Attachments));
    
  2. Retrieve the item attachment. The following code shows how to get the first attachment in the attachments collection, provided that the message has at least one attachment and the first attachment is an item attachment, load its properties, and print the subject and body of the attachment.

    if (message.HasAttachments && message.Attachments[0] is ItemAttachment)
    { 
       ItemAttachment itemAttachment = message.Attachments[0] as ItemAttachment;
       itemAttachment.Load();
       Console.WriteLine("Subject: " + itemAttachment.Item.Subject);
       Console.WriteLine("Body: " + itemAttachment.Item.Body);
    }
    

Example

The following code example shows how to iterate through the attachments collection of an existing message and get each attachment. File attachments are loaded in three different ways: into memory, into a file, and into a stream. Item attachments are loaded into memory. This example assumes that service is a valid ExchangeService binding. The ItemId has been shortened to preserve readability.

// Bind to an existing message item, requesting its Id property plus its attachments collection.
EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkA"), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in message.Attachments)
{
    if (attachment is FileAttachment)
    {
        FileAttachment fileAttachment = attachment as FileAttachment;
        
        // Load the file attachment into memory and print out its file name.
        fileAttachment.Load();
        Console.WriteLine("Attachment name: " + fileAttachment.Name);

        // Load attachment contents into a file.
        fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);

        // Stream attachment contents into a file.
        FileStream theStream = new FileStream("C:\\temp\\Stream_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        fileAttachment.Load(theStream);
        theStream.Close();
        theStream.Dispose();
    }
    else // Attachment is an item attachment.
    {
        // Load attachment into memory and write out the subject.
        ItemAttachment itemAttachment = attachment as ItemAttachment;
        itemAttachment.Load();
        Console.WriteLine("Subject: " + itemAttachment.Item.Subject);
    }
}

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.