Partager via


Replying to email messages by using the EWS Managed API 2.0

You can use the Microsoft Exchange Web Services (EWS) Managed API to reply to 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.

You can use one of two methods to reply to email messages: the Reply method or the CreateReply method. Use the CreateReply method to add recipients.

To reply to an email message by using the Reply method

  • Create the message body reply and indicate the reply type. Send the message reply.

    string myReply = "This is the message body of the email reply.";
    bool replyToAll = true;
    messageToReplyTo.Reply(myReply, replyToAll);
    

    Note

    This example assumes that the messageToReplyTo object is a valid EmailMessage object.

To reply to an email message by using the CreateReply method

  1. Create the reply response message from the original email message and indicate whether the message is a reply or reply all type of reply. The follow code example shows a reply type of response.

    bool replyToAll = false;
    ResponseMessage responseMessage = messageToReplyTo.CreateReply(replyToAll);
    
  2. Prefix the reply to the message body.

    string myReply = "This is the message body of the email reply.";
    responseMessage.BodyPrefix = myReply;
    
  3. Optional step: Add additional recipients to the email message reply.

    responseMessage.CcRecipients.Add("user4@contoso.com");
    
  4. Send and save a copy of the replied email message in the default Sent Items folder.

    responseMessage.SendAndSaveCopy();
    

The following table lists additional methods that you can use to send email message replies.

Method

Description

Send

Use to send an email message reply without saving a copy.

SendAndSaveCopy(WellKnownFolderName)

Use to send an email message reply and save a copy of the message to a default folder.

SendAndSaveCopy(FolderId)

Use to send an email message reply and save a copy of the message to a folder.

Example

The following examples show how to reply to a message.

Reply method example

The following example shows how to send an email reply to all the recipients and the sender of the original email message. This example assumes that the messageToReplyTo object is a valid EmailMessage object that has been set with a valid ExchangeService binding.

// Create the message body reply. Indicate whether the message is a
// reply or reply all type of reply, and then send the reply.
string myReply = "This is the message body of the email reply.";
bool replyToAll = true;
messageToReplyTo.Reply(myReply, replyToAll);

The following example shows the XML that is sent by using the Reply method. The ReferenceItemId Id and ChangeKey attributes have been shortened to preserve 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" />
    </soap:Header>
    <soap:Body>
      <m:CreateItem MessageDisposition="SendAndSaveCopy">
        <m:Items>
          <t:ReplyAllToItem>
            <t:ReferenceItemId Id="AAMkADdlZDg5EAAA=" ChangeKey="CQAAAY3AAAJzqms" />
            <t:NewBodyContent BodyType="Text">This is the message body of the email reply.</t:NewBodyContent>
          </t:ReplyAllToItem>
        </m:Items>
      </m:CreateItem>
    </soap:Body>
  </soap:Envelope>

The following example shows the XML that is returned by using the Reply method.

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Header>
      <t:ServerVersionInfo MajorVersion="8" 
                           MinorVersion="1" 
                           MajorBuildNumber="333" 
                           MinorBuildNumber="0" 
                           Version="Exchange2007_SP1" 
                           xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" />
    </soap:Header>
    <soap:Body>
      <m:CreateItemResponse xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" 
                            xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages">
        <m:ResponseMessages>
          <m:CreateItemResponseMessage ResponseClass="Success">
            <m:ResponseCode>NoError</m:ResponseCode>
            <m:Items />
          </m:CreateItemResponseMessage>
        </m:ResponseMessages>
      </m:CreateItemResponse>
    </soap:Body>
  </soap:Envelope>

CreateReply method example

The following example shows how to send an email reply to the sender of the original email message and adds user4 to the recipients. This example assumes that the messageToReplyTo object is a valid EmailMessage object that has been set with a valid ExchangeService binding.

// Create the reply response message from the original email message.
// Indicate whether the message is a reply or reply all type of reply.
bool replyToAll = false;
ResponseMessage responseMessage = messageToReplyTo.CreateReply(replyToAll);

// Prepend the reply to the message body. 
string myReply = "This is the message body of the email reply."; 
responseMessage.BodyPrefix = myReply;

// Add additional recipients to the reply email message. 
responseMessage.CcRecipients.Add("user4@contoso.com");

// Send and save a copy of the replied email mesage in the default Sent Items folder. 
responseMessage.SendAndSaveCopy();

The following example shows the XML that is sent by using the CreateReply method. The ReferenceItemId Id and ChangeKey attributes have been shortened to preserve 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" />
    </soap:Header>
    <soap:Body>
      <m:CreateItem MessageDisposition="SendAndSaveCopy">
        <m:Items>
          <t:ReplyToItem>
            <t:CcRecipients>
              <t:Mailbox>
                <t:EmailAddress>user4@contoso.com</t:EmailAddress>
              </t:Mailbox>
            </t:CcRecipients>
            <t:ReferenceItemId Id="AAMkADdlZDgonEAAA=" ChangeKey="CQAzqm4" />
            <t:NewBodyContent BodyType="Text">This is the message body of the email reply.</t:NewBodyContent>
          </t:ReplyToItem>
        </m:Items>
      </m:CreateItem>
    </soap:Body>
  </soap:Envelope>

The following example shows the XML that is returned by using the CreateReply method.

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" 
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Header>
      <t:ServerVersionInfo MajorVersion="8" 
                           MinorVersion="1" 
                           MajorBuildNumber="333" 
                           MinorBuildNumber="0" 
                           Version="Exchange2007_SP1" 
                           xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" />
    </soap:Header>
    <soap:Body>
      <m:CreateItemResponse xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" 
                            xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages">
        <m:ResponseMessages>
          <m:CreateItemResponseMessage ResponseClass="Success">
            <m:ResponseCode>NoError</m:ResponseCode>
            <m:Items />
          </m:CreateItemResponseMessage>
        </m:ResponseMessages>
      </m:CreateItemResponse>
    </soap:Body>
  </soap:Envelope>

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.