Partager via


Setting automatic replies by using the EWS Managed API 2.0

You can use the Microsoft Exchange Web Services (EWS) Managed API to set automatic replies, such as Out of Office (OOF) messages, for an email client and configure the automatic replies to be sent both within and outside a client's organization.

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.

To set an automatic reply

  1. Instantiate the OofSettings object.

    OofSettings myOOF = new OofSettings();
    
  2. Set the properties on the OofSettings object. The following example shows how to schedule an OOF message to be sent to the senders of incoming email messages both within and outside a client's organization.

    // Set the OOF status to be a scheduled time period.
    myOOF.State = OofState.Scheduled;
    
    // Select the time period during which to send OOF messages.
    myOOF.Duration = new TimeWindow(DateTime.Now.AddDays(4), DateTime.Now.AddDays(5));
    
    // Select the external audience that will receive OOF messages.
    myOOF.ExternalAudience = OofExternalAudience.All;
    
    // Set the OOF message for your internal audience.
    myOOF.InternalReply = new OofReply("I'm out of office. I'll be back tomorrow. Thanks!");
    
    // Set the OOF message for your external audience.
    myOOF.ExternalReply = new OofReply("I'm out of the office but will reply to emails when I return. Thanks!");
    
  3. Save the OOF message settings, as shown in the following example.

    // Set the selected values.
    service.SetUserOofSettings("user1@contoso.com", myOOF);
    

Example

The following example shows how to set and display automatic reply information.

static void SetOOF(ExchangeService service)
{
    OofSettings myOOF = new OofSettings();

    // Set the OOF status to be a scheduled time period.
    myOOF.State = OofState.Scheduled;
    
    // Select the time period to be OOF.
    myOOF.Duration = new TimeWindow(DateTime.Now.AddDays(4), DateTime.Now.AddDays(5));

    // Select the external audience that will receive OOF messages.
    myOOF.ExternalAudience = OofExternalAudience.All;

    // Set the OOF message for your internal audience.
    myOOF.InternalReply = new OofReply("I'm currently out of office. Please contact my manager for critical issues. Thanks!");

    // Set the OOF message for your external audience.
    myOOF.ExternalReply = new OofReply("I am currently out of the office but will reply to emails when I return. Thanks!");

    // Set the selected values. This method will result in a call to the Exchange server.
    service.SetUserOofSettings("someone@example.com", myOOF);
}

The following example shows the XML that is sent by using the SetUserOofSettings method.

  <?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="Exchange2010_SP1" />
    </soap:Header>
    <soap:Body>
      <m:SetUserOofSettingsRequest>
        <t:Mailbox>
          <t:Address>someone@example.com</t:Address>
        </t:Mailbox>
        <t:UserOofSettings>
          <t:OofState>Scheduled</t:OofState>
          <t:ExternalAudience>All</t:ExternalAudience>
          <t:Duration>
            <t:StartTime>2011-10-29T18:45:08.243Z</t:StartTime>
            <t:EndTime>2011-10-30T18:45:08.243Z</t:EndTime>
          </t:Duration>
          <t:InternalReply xml:lang="en-US">
            <t:Message>I'm currently out of office. Please contact my manager for critical issues. Thanks!</t:Message>
          </t:InternalReply>
          <t:ExternalReply xml:lang="en-US">
            <t:Message>I am currently out of the office but will reply to emails when I return. Thanks!</t:Message>
          </t:ExternalReply>
        </t:UserOofSettings>
      </m:SetUserOofSettingsRequest>
    </soap:Body>
  </soap:Envelope>

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

  <?xml version="1.0" encoding="utf-8"?>
  <s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
      <h:ServerVersionInfo MajorVersion="14" MinorVersion="15" MajorBuildNumber="3" MinorBuildNumber="0" Version="Exchange2010_SP2" xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types"xmlns="https://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
    </s:Header>
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <SetUserOofSettingsResponse xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
        <ResponseMessage ResponseClass="Success">
          <ResponseCode>NoError</ResponseCode>
        </ResponseMessage>
      </SetUserOofSettingsResponse>
    </s:Body>
  </s: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.