Share via


Creating E-mail Messages (Exchange Web Services)

Topic Last Modified: 2008-01-02

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

Example

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

static void CreateEmail()
{
    // 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 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 = "user1@example.com";
    message.ToRecipients = new EmailAddressType[1];
    message.ToRecipients[0] = new EmailAddressType();
    message.ToRecipients[0].EmailAddress = "user2@example.com";
    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;

    try
    {
        // 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);
        }
        else
        {
            Console.WriteLine("Item was created");
        }

    }
    catch(Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

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="http://www.w3.org/2001/XMLSchema-instance" 
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                  MessageDisposition="SendAndSaveCopy">
  <SavedItemFolderId xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <DistinguishedFolderId Id="sentitems" xmlns="https://schemas.microsoft.com/exchange/services/2006/types" />
  </SavedItemFolderId>
  <Items xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <Message xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
      <ItemClass>IPM.Note</ItemClass>
      <Subject>Daily Report</Subject>
      <Body BodyType="Text">(1) Handled customer issues, (2) Saved the world.</Body>
      <Sender>
        <Mailbox>
          <EmailAddress>user1@example.com</EmailAddress>
        </Mailbox>
      </Sender>
      <ToRecipients>
        <Mailbox>
          <EmailAddress>user2@example.com</EmailAddress>
        </Mailbox>
      </ToRecipients>
    </Message>
  </Items>
</CreateItem>

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="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">
    <CreateItemResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <Items />
    </CreateItemResponseMessage>
  </ResponseMessages>
</CreateItemResponseType>

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 Exchange Web Services Client Development.

See Also

Reference

CreateItemType

Other Resources

CreateItem Operation