Partager via


Forwarding email messages by using the EWS Managed API 2.0

You can use the Microsoft Exchange Web Services (EWS) Managed API to forward email messages.

Last modified: October 13, 2012

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.

Two methods are available for forwarding email messages: the Forward method and the CreateForward method. The Forward method only forwards email messages to recipients on the To: line. The CreateForward method forwards email messages to recipients on the To:, Cc:, and Bcc: lines.

To forward an email message by using the Forward method

  1. Create the email message body prefix for the email message that is to be forwarded. The following code shows an example of a message body prefix.

    string messageBodyPrefix = "This message was forwarded by using the EWS Managed API";
    
  2. Identify the addresses that will receive the forwarded email message.

    EmailAddress[] addresses = new EmailAddress[1];
    addresses[0] = new EmailAddress("user1@contoso.com");
    
  3. Forward the email message. Add the email message body prefix and addresses to the forwarded email message.

    emailMessage.Forward(messageBodyPrefix, addresses);
    

This procedure assumes that you have a valid EmailMessage object that represents an email message item in the Exchange database. The procedure also assumes that the URL for Exchange Web Services has been set on the EmailMessage object.

To forward an email message by using the CreateForward method

  1. Create the email message body prefix for the email message that is to be forwarded. The following code shows an example of a message body prefix.

    string messageBodyPrefix = "This message was forwarded by using the EWS Managed API";
    
  2. Create the forward response message from the original email message.

    ResponseMessage responseMessage = emailMessage.CreateForward();
    
  3. Add recipients to the forwarded email message.

    responseMessage.ToRecipients.Add("user1@contoso.com");
    responseMessage.CcRecipients.Add("user2@contoso.com");
    responseMessage.BccRecipients.Add("user3@contoso.com");
    
  4. Prepend the email message body.

    // Prepend the email message body.
    responseMessage.BodyPrefix = messageBodyPrefix;
    
  5. Send the forwarded email message and save a copy of the message in the default Sent Items folder.

    responseMessage.SendAndSaveCopy();
    

This procedure assumes that you have a valid EmailMessage object that represents an email message item in the Exchange database. The procedure also assumes that the URL for Exchange Web Services has been set on the EmailMessage object.

Example

The following code example shows how to forward an email message by using the Forward method. This example assumes that the EmailMessage object named emailMessage has been bound to an object in the Exchange database and that the service URL has been set on the EmailMessage object.

// Create the prefixed content to add to the forwarded message body.
string messageBodyPrefix = "This is message that was forwarded by using the EWS Managed API";

// Create the addresses that the forwarded email message is to be sent to.
EmailAddress[] addresses = new EmailAddress[1];
addresses[0] = new EmailAddress("user1@contoso.com");

// Send the forwarded message.
emailMessage.Forward(messageBodyPrefix, addresses);

The following code example shows how to forward an email message by using the CreateForward method. This example assumes that the EmailMessage object named emailMessage has been bound to an object in the Exchange database and that the service URL has been set on the EmailMessage object.

// Create the prefixed content to add to the forwarded message body.
string messageBodyPrefix = "This is a message that was forwarded by using the EWS Managed API";

// Create the forward response message.
ResponseMessage responseMessage = emailMessage.CreateForward();

// Prepend the email message body.
responseMessage.BodyPrefix = messageBodyPrefix;

// Add the recipients who are to receive the forwarded email message.
responseMessage.ToRecipients.Add("user1@contoso.com");
responseMessage.CcRecipients.Add("user2@contoso.com");
responseMessage.BccRecipients.Add("user3@contoso.com");

// Send the forwarded email message and save a copy of the message in the default Sent Items folder.
responseMessage.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.