Creating E-mail Messages in Exchange 2010

Last modified: May 21, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

You can use Exchange Web Services to create and send e-mail messages.


The following example shows you how to create and send an e-mail message.

static void CreateEmail(ExchangeServiceBinding esb)
    // Create the CreateItem request.
    CreateItemType createItemRequest = new CreateItemType();

    // Specifiy how the created items are handled
    createItemRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
    createItemRequest.MessageDispositionSpecified = true;

    // Specify the location of sent items. 
    createItemRequest.SavedItemFolderId = new TargetFolderIdType();
    DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
    sentitems.Id = DistinguishedFolderIdNameType.sentitems;
    createItemRequest.SavedItemFolderId.Item = sentitems;

    // Create the array of items.
    createItemRequest.Items = new NonEmptyArrayOfAllItemsType();

    // Create a single e-mail message.
    MessageType message = new MessageType();
    message.Subject = "Daily Report";
    message.Body = new BodyType();
    message.Body.BodyType1 = BodyTypeType.Text;
    message.Body.Value = "(1) Handled customer issues, (2) Saved the world.";
    message.ItemClass = "IPM.Note";
    message.Sender = new SingleRecipientType();
    message.Sender.Item = new EmailAddressType();
    message.Sender.Item.EmailAddress = "";
    message.ToRecipients = new EmailAddressType[1];
    message.ToRecipients[0] = new EmailAddressType();
    message.ToRecipients[0].EmailAddress = "";
    message.Sensitivity = SensitivityChoicesType.Normal;

    // Add the message to the array of items to be created.
    createItemRequest.Items.Items = new ItemType[1];
    createItemRequest.Items.Items[0] = message;

        // Send the request to create and send the e-mail item, and get the response.
        CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);

        // Determine whether the request was a success.
        if (createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
            throw new Exception(createItemResponse.ResponseMessages.Items[0].MessageText);
            Console.WriteLine("Item was created");

    catch(Exception e)

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" ?>
<CreateItem xmlns:xsi="" 
  <SavedItemFolderId xmlns="">
    <DistinguishedFolderId Id="sentitems" xmlns="" />
  <Items xmlns="">
    <Message xmlns="">
      <Subject>Daily Report</Subject>
      <Body BodyType="Text">(1) Handled customer issues, (2) Saved the world.</Body>

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" ?>
<CreateItemResponseType xmlns:xsi="" 
  <ResponseMessages xmlns="">
    <CreateItemResponseMessage ResponseClass="Success">
      <Items />

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.

Compiling the Code

For information about compiling the code, see EWS Client Development in Exchange 2010.

See Also


Other Resources