Respond to email messages by using EWS in Exchange

Learn how to respond to email messages by using the EWS Managed API or EWS in Exchange.

You can use the EWS Managed API or EWS to respond to messages by replying to them or forwarding them to recipients.

Table 1. EWS Managed API methods and EWS operations for responding to email messages

Task EWS Managed API method EWS operation
Reply to an email message
EmailMessage.Reply
EmailMessage.CreateReply
CreateItem, where the Items element has a child element of either ReplyToItem or ReplyAllToItem.
Forward an email message
EmailMessage.Forward
EmailMessage.CreateForward
CreateItem, where the Items element has a child element of ForwardItem.

Reply to an email message by using the EWS Managed API

The EWS Managed API provides two methods that you can use to respond to messages: Reply and CreateReply. The Reply method only takes two parameters: the response message to prepend to the existing body, and a Boolean value that indicates whether the response should go to all recipients (true) or just the sender (false). If you need to add additional recipients to a message, set additional properties on a response, or add an attachment, use the CreateReply method, which enables you to set all the first-class properties that are available on an EmailMessage object.

The following code example shows how to use the Reply method to respond to an email message.

This example assumes that service is a valid ExchangeService object and that the user has been authenticated to an Exchange server. The local variable ItemId is the Id of the item to respond to. The example calls the FindRecentlySent method to verify that the message was marked as replied to.

// As a best practice, limit the properties returned by the Bind method to only those that are required.
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.LastModifiedTime);
// Bind to the email message to reply to by using the ItemId.
// This method call results in a GetItem call to EWS.
EmailMessage message = EmailMessage.Bind(service, ItemId, propSet);
string myReply = "This is the message body of the email reply.";
bool replyToAll = false;
// Send the response message.
// This method call results in a CreateItem call to EWS.
message.Reply(myReply, replyToAll);
// Verify that the response was sent by calling FindRecentlySent.
FindRecentlySent(message);

The following code example shows how to use the CreateReply method to respond to an email message.

// Bind to the email message to reply to by using the ItemId.
// This method call results in a GetItem call to EWS.
EmailMessage message = EmailMessage.Bind(service, ItemId, BasePropertySet.IdOnly);
// 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 = true;
ResponseMessage responseMessage = message.CreateReply(replyToAll);
// Prepend the reply to the message body. 
string myReply = "This is the message body of the email reply.";
responseMessage.BodyPrefix = myReply;
// Send the response message.
// This method call results in a CreateItem call to EWS.
responseMessage.SendAndSaveCopy();
// Check that the response was sent by calling FindRecentlySent.
FindRecentlySent(message);

If you need to add an attachment to the response message, replace the call to the SendAndSaveCopy method with the following code.

EmailMessage reply = responseMessage.Save();
reply.Attachments.AddFileAttachment("attachmentname.txt");
reply.Update(ConflictResolutionMode.AutoResolve);
reply.SendAndSaveCopy();

Reply to an email message by using EWS

The following code example shows how to reply to a message by using EWS. Use the CreateItem operation with the MessageDisposition attribute set to SendAndSaveCopy to send the message and save the response in the Sent Items folder. Include either the ReplyAllToItem element as a child of the Items element to reply to everyone on the message thread, or include the ReplyToItem element to reply only to the sender.

This is also the XML request that the EWS Managed API sends when calling either the Reply or the CreateReply method.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
               xmlns:t="http://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="AAMkADE4="
                             ChangeKey="CQAAABYA" />
          <t:NewBodyContent BodyType="HTML">This is the message body of the email reply.</t:NewBodyContent>
        </t:ReplyAllToItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

The server responds to the CreateItem request with a CreateItemResponse message that includes a ResponseCode element value of NoError, which indicates that the reply was created and sent successfully.

If you need to add an attachment to your response message, call the CreateItem operation as specified above, but change the MessageDisposition to SaveOnly. Then call the CreateAttachment operation, followed by the SendItem operation.

Forward an email message by using the EWS Managed API

The EWS Managed API provides two methods that you can use to forward messages: Forward and CreateForward. The Forward method only takes two parameters: the message to prepend to the existing body, and an array or collection of recipients, depending on the overload you choose to use. If you need to add an attachment to the message you're forwarding, or set additional properties on the new message, use the CreateForward method, which enables you to set all the properties that are available on an EmailMessage object.

The following code example shows how to use the Forward method to forward an email message to one recipient.

This example assumes that service is a valid ExchangeService object and that the user has been authenticated to an Exchange server. The local variable ItemId is the Id of the item to forward. The example calls the FindRecentlySent method to verify that the message was marked as forwarded.

// Bind to the email message to reply to by using the ItemId.
// This method call results in a GetItem call to EWS.
EmailMessage message = EmailMessage.Bind(service, ItemId, BasePropertySet.IdOnly);
string myForward = "This is the message body of the forwarded email.";
// Send the response message.
// This method call results in a CreateItem call to EWS.
message.Forward(myForward, "sadie@contoso.com");
// Verify that the forwarded response was sent by calling FindRecentlySent.
FindRecentlySent(message);

The following code example shows how to use the CreateForward method to forward an email message to one recipient.

// Bind to the email message to reply to by using the ItemId.
// This method call results in a GetItem call to EWS.
EmailMessage message = EmailMessage.Bind(service, ItemId, BasePropertySet.IdOnly);
// Create the reply response message from the original email message.
// Indicate whether the message is a reply or reply all type of reply.
ResponseMessage forwardMessage = message.CreateForward();
// Set properties on the email message.
forwardMessage.ToRecipients.Add("sadie@contoso.com");
forwardMessage.Body = "Sadie,<br><br>I thought you'd be interested in this thread.<br><br>-Mack";
// Send and save a copy of the replied email message in the default Sent Items folder. 
forwardMessage.SendAndSaveCopy();
// Verify that the forwarded message was sent by calling FindRecentlySent.
FindRecentlySent(message);

If you need to add an attachment to the forwarded message, replace the call to the SendAndSaveCopy method with the following code.

EmailMessage forward = forwardMessage.Save();
forward.Attachments.AddFileAttachment("attachmentname.txt");
forward.Update(ConflictResolutionMode.AutoResolve);
forward.SendAndSaveCopy();

Forward an email message by using EWS

The following code example shows how to forward a message by using EWS. Use the CreateItem operation with the MessageDisposition attribute set to SendAndSaveCopy to send the message and save the response in the Sent Items folder. The ForwardItem element indicates that the item is a forwarded message.

This is also the XML request that the EWS Managed API sends when calling either the Forward or the CreateForward method.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
               xmlns:t="http://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:ForwardItem>
          <t:ToRecipients>
            <t:Mailbox>
              <t:EmailAddress>sadie@contoso.com</t:EmailAddress>
            </t:Mailbox>
          </t:ToRecipients>
          <t:ReferenceItemId Id="AAAMkADE="
                             ChangeKey="CQAAABYA" />
          <t:NewBodyContent BodyType="HTML">This is the message body of the forwarded email.</t:NewBodyContent>
        </t:ForwardItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

The server responds to the CreateItem request with a CreateItemResponse message that includes a ResponseCode element value of NoError, which indicates that the forwarded message was created and sent successfully.

If you need to add an attachment to your response message, call the CreateItem operation, but change the MessageDisposition to SaveOnly. Then call the CreateAttachment operation, followed by the SendItem operation.

Find the message last replied to or forwarded by using the EWS Managed API

The following code example shows how to find the last verb executed and the time the last verb was executed on the item specified. This method is called from other EWS Managed API code examples in this topic to verify that the items you replied to or forwarded were marked as replied to or forwarded in your Inbox.

The example uses the PidTagLastVerbExecuted (0x10820003) extended property to determine whether the message was a reply, a reply all, or a forward, and the PidTagLastVerbExecutionTime (0x10820040) extended property to determine when the reply or forward was sent.

public static void FindRecentlySent(EmailMessage messageToCheck)
{
    // Create extended property definitions for PidTagLastVerbExecuted and PidTagLastVerbExecutionTime.
    ExtendedPropertyDefinition PidTagLastVerbExecuted = new ExtendedPropertyDefinition(0x1081, MapiPropertyType.Integer);
    ExtendedPropertyDefinition PidTagLastVerbExecutionTime = new ExtendedPropertyDefinition(0x1082, MapiPropertyType.SystemTime);
    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Subject, PidTagLastVerbExecutionTime, PidTagLastVerbExecuted);
    messageToCheck = EmailMessage.Bind(service, messageToCheck.Id, propSet);
    // Determine the last verb executed on the message and display output.
    object responseType;
    if (messageToCheck.TryGetProperty(PidTagLastVerbExecuted, out responseType))
    {
        object ReplyTime = null;
        switch (((Int32)responseType))
        {
            case 102: Console.WriteLine("A reply was sent to the '" + messageToCheck.Subject.ToString() + "' email message at");
                break;
            case 103: Console.WriteLine("A reply all was sent to the '" + messageToCheck.Subject.ToString() + "' email message at");
                break;
            case 104: Console.WriteLine("The '" + messageToCheck.Subject.ToString() + "' email message was forwarded at");
                break;
        }
        if (messageToCheck.TryGetProperty(PidTagLastVerbExecutionTime, out ReplyTime))
        {
            Console.WriteLine(((DateTime)ReplyTime).ToString() + ".");
        }
    }
    else
    {
        Console.WriteLine("No changes were made to  '" + messageToCheck.Subject.ToString() + "'.");
    }
}

See also