다음을 통해 공유


Synchronizing Mailboxes (Exchange Web Services)

Topic Last Modified: 2007-11-01

You can use Exchange Web Services to synchronize items between a computer that is running Microsoft Exchange Server 2007 and the client.

Example

This example shows you how to use the SyncFolderItems Operation to synchronize items.

static void SyncItems(string syncState)
{
    // Create service binding.
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.Credentials = new NetworkCredential("username", "password", "domain");
    esb.Url = @"https://CAS01.example.com/EWS/exchange.asmx";

    // Create the synchronize items request.
    SyncFolderItemsType syncItemsRequest = new SyncFolderItemsType();
    
    // Specify the properties to synchronize.
    syncItemsRequest.ItemShape = new ItemResponseShapeType();
    syncItemsRequest.ItemShape.BaseShape = DefaultShapeNamesType.Default;

    // Specify the folder to synchronize.
    syncItemsRequest.SyncFolderId = new TargetFolderIdType();
    DistinguishedFolderIdType drafts = new DistinguishedFolderIdType();
    drafts.Id = DistinguishedFolderIdNameType.drafts;
    syncItemsRequest.SyncFolderId.Item = drafts;

    // Specify the synchronization state if this is not the first attempt to
    // synchronize the client and the Exchange server.
    if (syncState.Length != 0)
    {
        syncItemsRequest.SyncState = syncState;
    }

    // Specify the number of items to return.
    syncItemsRequest.MaxChangesReturned = 20;

    try
    {
        // Send the request and get the response.
        SyncFolderItemsResponseType syncItemsResponse = esb.SyncFolderItems(syncItemsRequest);

        SyncFolderItemsResponseMessageType responseMessage = new SyncFolderItemsResponseMessageType();
        responseMessage = syncItemsResponse.ResponseMessages.Items[0] as SyncFolderItemsResponseMessageType;

        // Determine whether the request was a success.
        if (responseMessage.ResponseClass == ResponseClassType.Error)
        {
            throw new Exception(responseMessage.MessageText);
        }
        else
        {
            string changes = responseMessage.Changes.Items.Length.ToString();
            Console.WriteLine("Number of items to synchronize: " + changes);
        }
    }
    
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<SyncFolderItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ItemShape xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <BaseShape xmlns="https://schemas.microsoft.com/exchange/services/2006/types">Default</BaseShape>
  </ItemShape>
  <SyncFolderId xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <DistinguishedFolderId Id="drafts" xmlns="https://schemas.microsoft.com/exchange/services/2006/types" />
  </SyncFolderId>
  <MaxChangesReturned xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">20</MaxChangesReturned>
</SyncFolderItems>
<?xml version="1.0" encoding="utf-8" ?>
<SyncFolderItemsResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResponseMessages xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <SyncFolderItemsResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <SyncState>H4sIAAAA=</SyncState>
      <IncludesLastItemInRange>true</IncludesLastItemInRange>
      <Changes>
        <Create xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <Message>
            <ItemId Id="AAAlAFVzA" ChangeKey="CQAAfYAD" />
            <Subject>Test</Subject>
            <Sensitivity>Normal</Sensitivity>
            <Size>879</Size>
            <DateTimeSent>2006-10-27T23:20:39Z</DateTimeSent>
            <DateTimeCreated>2006-10-27T23:39:03Z</DateTimeCreated>
            <HasAttachments>false</HasAttachments>
            <From>
              <Mailbox>
                <Name>User1</Name>
                <EmailAddress>User1@example.com</EmailAddress>
                <RoutingType>SMTP</RoutingType>
              </Mailbox>
            </From>
            <IsRead>true</IsRead>
          </Message>
        </Create>
        <Create xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <Message>
            <ItemId Id="AAAlAFVz" ChangeKey="CQAY+Z" />
            <Subject>Project Action</Subject>
            <Sensitivity>Normal</Sensitivity>
            <Size>484</Size>
            <DateTimeSent>2006-11-02T23:49:31Z</DateTimeSent>
            <DateTimeCreated>2006-11-02T23:49:31Z</DateTimeCreated>
            <HasAttachments>false</HasAttachments>
            <From>
              <Mailbox>
                <Name>User1</Name>
                <EmailAddress>User1@example.com</EmailAddress>
                <RoutingType>SMTP</RoutingType>
              </Mailbox>
            </From>
            <IsRead>true</IsRead>
          </Message>
        </Create>
      </Changes>
    </SyncFolderItemsResponseMessage>
  </ResponseMessages>
</SyncFolderItemsResponse>

The SyncState and item identifiers have been shortened to preserve readability.