Creating and sending email messages by using the EWS Managed API 2.0

You can use the Microsoft Exchange Web Services (EWS) Managed API to create and send email messages.

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 create and send an email message

  1. Create an email message and identify the Exchange service. The following code shows how to create an email message and provide it with connection configuration information by using an ExchangeService object named service.

    EmailMessage message = new EmailMessage(service);
    
  2. Add properties to the email message. The following code shows how to add a subject, a body, and a single recipient to an email message.

    message.Subject = "Interesting";
    
    message.Body = "The proposition has been considered.";
    
    message.ToRecipients.Add("user1@contoso.com");
    
  3. Send the email message and save a copy. The following code shows how to send an email message and save a copy of the message to the Sent Items folder.

    message.SendAndSaveCopy();
    

Example

The following example shows how to create and send an email message to user1@contoso.com.

// Create an email message and identify the Exchange service.
EmailMessage message = new EmailMessage(service);

// Add properties to the email message.
message.Subject = "Interesting";
message.Body = "The merger is finalized.";
message.ToRecipients.Add("user1@contoso.com");

// Send the email message and save a copy.
message.SendAndSaveCopy();
' Create an email message and identify the Exchange service. 
Dim message As New EmailMessage(service) 

' Add properties to the email message. 
message.Subject = New String("Interesting")
message.Body = New String("The merger is finalized.") 
message.ToRecipients.Add("user1@contoso.com")

' Send the email message and save a copy. 
message.SendAndSaveCopy()

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

<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" />
  </soap:Header>
  <soap:Body>
    <m:CreateItem MessageDisposition="SendAndSaveCopy">
      <m:SavedItemFolderId>
        <t:DistinguishedFolderId Id="sentitems" />
      </m:SavedItemFolderId>
      <m:Items>
        <t:Message>
          <t:Subject>Interesting</t:Subject>
          <t:Body BodyType="HTML">The merger is finalized.</t:Body>
          <t:ToRecipients>
            <t:Mailbox>
              <t:EmailAddress>
                user1@contoso.com </t:EmailAddress>
              </t:Mailbox>
          </t:ToRecipients>
        </t:Message>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

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

<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="14" 
               MinorVersion="0" 
               MajorBuildNumber="639" 
               MinorBuildNumber="20" 
               Version="Exchange2010" 
               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" 
               xm=""lns: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">
    <m:CreateItemResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
               xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:CreateItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items />
        </m:CreateItemResponseMessage>
      </m:ResponseMessages>
    </m:CreateItemResponse>
  </s:Body>
</s:Envelope>

This example assumes that the ExchangeService object is correctly configured for connecting to the user's Client Access server.

You can expand this example in many ways. The email message can be set with many more properties, such as attachments, Bcc recipients, categories, sender, and item class. The email message can be set with all the MIME content by using the MimeContent property.

This example shows how to send an email message and save a copy of the message in the default Sent Items folder by using the SendAndSaveCopy() method. The following table lists some additional methods that you can use to send email messages.

Method

Action

Send()

Send an email message without saving a copy.

SendAndSaveCopy(WellKnownFolderName)

Send an email message and save a copy of the message to a default folder.

SendAndSaveCopy(FolderId)

Send an email message and save a copy of the message to a folder.

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.