Sending E-mail Messages (Exchange Web Services)

Topic Last Modified: 2007-09-14

You can use Exchange Web Services to send items that have defined recipients.

Example

The following example shows you how to send an item in the Exchange store.

static void Main()
{
    // Create the bindings and set the credentials.
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.Url = "http://myExchangeServer/ews/exchange.asmx";

    esb.Credentials = new NetworkCredential("UserName", "Password", "Domain");

    // Create the SendItem request.
    SendItemType sit = new SendItemType();
    sit.ItemIds = new BaseItemIdType[1];

    // Create an item ID type and set the message ID and change key.
    ItemIdType itemId = new ItemIdType();

    // Get the ID and change key from the message that you obtained by using FindItem or CreateItem.
    itemId.Id = "AQAtA=";
    itemId.ChangeKey = "CQAAAB";

    sit.ItemIds[0] = itemId;

    // Send the message.
    SendItemResponseType siResponse = esb.SendItem(sit);
    
    // Check the result.
    if (siResponse.ResponseMessages.Items.Length > 0 &&
        siResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
    {
        Console.WriteLine("Message with Id {0} and ChangeKey {1} is sent.", itemId.Id, itemId.ChangeKey);
    }
}

The following XML example shows the XML request message that is sent from the client to the server.

<?xml version="1.0" encoding="utf-8"?>
<SendItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  SaveItemToFolder="false">
  <ItemIds xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <ItemId 
            Id="AQAtA=" 
            ChangeKey="CQAAAB" 
            xmlns="https://schemas.microsoft.com/exchange/services/2006/types" />
  </ItemIds>
</SendItem>

The following XML example shows the XML response message that is sent from the server to the client.

<?xml version="1.0" encoding="utf-8"?>
<SendItemResponse 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">
    <SendItemResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
    </SendItemResponseMessage>
  </ResponseMessages>
</SendItemResponse> 

The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by Microsoft Visual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.

Note

The item identifier and change key have been shortened to preserve readability.

Compiling the Code

For information about compiling the code, see Exchange Web Services Client Development.

Robust Programming

For information about robust programming, see Exchange Web Services Architecture.

See Also

Other Resources

SendItem Operation
SendItem
SendItemResponse