Share via


Creating attachments by using the EWS Managed API 2.0

You can use the Exchange Web Services (EWS) Managed API to add attachments to an item. Attachments can be either files or 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 create a file attachment on a new message

  1. Instantiate the Message object that will contain the file attachment, identify the Exchange service, and set the properties on the message. The following code shows how to create a message, provide it with connection configuration information by using an ExchangeService object named service, and set the subject, body, and recipients for the message.

    EmailMessage message = new EmailMessage(service);
    message.Subject = "Message with Attachments";
    message.Body = "This message contains four file attachments.";
    message.ToRecipients.Add("User1@contoso.com");
    
  2. Add the file attachment to the email message. The following code shows four different ways to add file attachments to an email message.

    // Add an attachment by using the fully qualified string name. The email attachment is named FileAttachment1.txt.
    message.Attachments.AddFileAttachment("C:\\temp\\FileAttachment1.txt");
    
    // Add an attachment by using the fully qualified string name, and specify the name of the attachment. The email attachment is named SecondAttachment.txt.
    message.Attachments.AddFileAttachment("SecondAttachment.txt", "C:\\temp\\FileAttachment2.txt");
    
    // Add an attachment by using a byte array, and specify the name of the attachment. The email attachment is named ThirdAttachment.jpg.
    // In this example, theBytes is the byte array that represents the content of the image file to attach.
    message.Attachments.AddFileAttachment("ThirdAttachment.jpg", theBytes);
    
    // Add an attachment by using a stream, and specify the name of the attachment. The email attachment is named FourthAttachment.txt.
    // In this example, theStream is a Stream object that represents the content of the file to attach.
    message.Attachments.AddFileAttachment("FourthAttachment.txt", theStream);
    
  3. Send the message and save a copy in the Sent Items folder.

    message.SendAndSaveCopy();
    

To create an item attachment on a new message

  1. Instantiate the Message object that will contain the item attachment, identify the Exchange service, and set the properties on the message. The following example shows how to create a message, provide it with connection configuration information by using an ExchangeService object named service, and set the subject, body, and recipients for the message.

    EmailMessage message = new EmailMessage(service);
    message.Subject = "Message with an Attachment";
    message.Body = "This message contains one item attachment.";
    message.ToRecipients.Add("User1@contoso.com");
    
  2. Create the item attachment and set properties on the item. Any type of item that can be created by using the EWS Managed API can be used as an item attachment. The following example shows how to create a message item attachment on an e-mail message. In this example, specifying EmailMessage makes the item attachment an email message item.

    ItemAttachment<EmailMessage> itemAttachment1 = message.Attachments.AddItemAttachment<EmailMessage>();
    itemAttachment1.Name = "Attached Message Item";
    itemAttachment1.Item.Subject = "Message Item Subject";
    itemAttachment1.Item.Body = "Message Item Body";
    itemAttachment1.Item.ToRecipients.Add("User1@contoso.com");
    
  3. Send the message and save a copy in the Sent Items folder.

    message.SendAndSaveCopy();
    

Example

The following code example shows how to create a new e-mail message with four file attachments and one message item attachment. This example assumes that service is a valid ExchangeService binding, and that the FileAttachment1.txt, FileAttachment2.txt, Tulip.jpg, and FileAttachment4.txt files are located in the folder specified by the path shown in the example.

// Create an email message and set properties on the message.
EmailMessage message = new EmailMessage(service);
// Subject
message.Subject = "Message with Attachments";
// Body
message.Body = "This message contains four file attachments and one message item attachment.";
// Recipients
message.ToRecipients.Add("User1@Contoso.com");
message.ToRecipients.Add("User2@Contoso.com");

// Add a file attachment by using the fully qualified string name. 
// The email attachment is named FileAttachment1.txt.
message.Attachments.AddFileAttachment("C:\\temp\\FileAttachment1.txt");

// Add a file attachment by using the fully qualified string name, and specify the name of the attachment.
// The email attachment is named SecondAttachment.txt.
message.Attachments.AddFileAttachment("SecondAttachment.txt", "C:\\temp\\FileAttachment2.txt");

// Add a file attachment by using a byte array, and specify the name of the attachment.
// The email attachment is named ThirdAttachment.jpg.
byte[] theBytes = File.ReadAllBytes("C:\\Temp\\Tulips.jpg");
// In this example, theBytes is the byte array that represents the content of the image file to attach.
message.Attachments.AddFileAttachment("ThirdAttachment.jpg", theBytes);

// Add a file attachment by using a stream, and specify the name of the attachment.
// The email attachment is named FourthAttachment.txt.
FileStream theStream = new FileStream("C:\\temp\\FileAttachment4.txt", FileMode.OpenOrCreate);
// In this example, theStream is a Stream object that represents the content of the file to attach.
message.Attachments.AddFileAttachment("FourthAttachment.txt", theStream);

// Add an email message item attachment and set properties on the item.
ItemAttachment<EmailMessage> itemAttachment = message.Attachments.AddItemAttachment<EmailMessage>();
itemAttachment.Name = "Attached Message Item";
itemAttachment.Item.Subject = "Message Item Subject";
itemAttachment.Item.Body = "Message Item Body";
itemAttachment.Item.ToRecipients.Add("User1@Contoso.com");
itemAttachment.Item.ToRecipients.Add("User2@Contoso.com");

// Send the mail and save a copy in the Sent Items folder. This results in a call to EWS.
message.SendAndSaveCopy();
'Create an email message and set properties on the message.
Dim message As New EmailMessage(service)
'Subject
message.Subject = "Message with Attachments"
'Body
message.Body = "This message contains four file attachments and one message item attachment."
'Recipients
message.ToRecipients.Add("User1@Contoso.com")
message.ToRecipients.Add("User2@Contoso.com")

'Add a file attachment by using the fully qualified string name. 
'The email attachment is named FileAttachment1.txt.
message.Attachments.AddFileAttachment("C:\temp\FileAttachment1.txt")

'Add a file attachment by using the fully qualified string name, and specify the name of the attachment.
'The email attachment is named SecondAttachment.txt.
message.Attachments.AddFileAttachment("SecondAttachment.txt", "C:\temp\FileAttachment2.txt")

'Add a file attachment by using a byte array, and specify the name of the attachment.
'The email attachment is named ThirdAttachment.jpg.
Dim theBytes As Byte() = File.ReadAllBytes("C:\Temp\Tulips.jpg")
'In this example, theBytes is the byte array that represents the content of the image file to attach.
message.Attachments.AddFileAttachment("ThirdAttachment.jpg", theBytes)

'Add a file attachment by using a stream, and specify the name of the attachment.
'The email attachment is named FourthAttachment.txt.
Dim theStream As New FileStream("C:\temp\FileAttachment4.txt", FileMode.OpenOrCreate)
'In this example, theStream is a Stream object that represents the content of the file to attach.
message.Attachments.AddFileAttachment("FourthAttachment.txt", theStream)

'Add an email message item attachment and set properties on the item.
Dim attachment As ItemAttachment(Of EmailMessage) = message.Attachments.AddItemAttachment(Of EmailMessage)()
attachment.Name = "Attached Message Item"
attachment.Item.Subject = "Message Item Subject"
attachment.Item.Body = "Message Item Body"
attachment.Item.ToRecipients.Add("User1@Contoso.com")
attachment.Item.ToRecipients.Add("User2@Contoso.com")

'Send the mail and save a copy in the Sent Items folder. This results in a call to EWS.
message.SendAndSaveCopy()

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.