8 out of 16 rated this helpful - Rate this topic

Getting started with Exchange Online

Exchange Server 2010

Summary:  Get started working with Exchange Online. Find information about how to create a console application that creates and sends an email message to an Exchange Online account.

Applies to:  Microsoft Exchange Online

Published:  February 2011

Provided by:  Steve Petersen, Microsoft Corporation

You can use Exchange Web Services (EWS) in Microsoft Exchange Server 2010 or the Microsoft Exchange Web Services (EWS) Managed API 1.1 to develop against Exchange Online. In this article, I show you how to use the EWS Managed API to create and send an email message from an Exchange Online account.

First steps

If you want to create an application that you can use create and send email from an Exchange Online account, there are a few simple tasks that you need to complete.

Download and install the EWS Managed API

The EWS Managed API provides a managed interface for developing client applications that use EWS. You can download and install the EWS Managed API from the Microsoft Download Center.

For more information about the EWS Managed API, see the Microsoft Exchange Web Services Managed API 1.1 SDK.

Verify your Exchange Online account on the service

Before you can develop against Exchange Online, an administrator must add you to the system by creating a user account. When the administrator creates your user account, a Windows Live ID is also created for you that includes your email address and password.

You can verify that you have an Exchange Online account by verifying that you can send and receive mail via any email client through the account. Contact your system administrator to ensure that you have the correct email alias, domain, and password for your Exchange Online account.

Select a development tool

Because the Exchange Web Services interfaces are based on the HTTP, XML, SOAP, and WSDL open standards, you can use any platform that supports sending and receiving HTTP requests and responses to develop against Exchange Online. Building on platforms that support Web Services and .NET Framework will provide for easiest solutions. For example, you can use Microsoft Visual Studio 2010. Visual Studio 2010 offers development tools for building Web service applications. In addition, you can use Visual Studio to build EWS applications based on the mailbox user who is connecting to the Web service.

Add a reference

In your Visual Studio project, add a reference to Microsoft.Exchange.WebServices.dll and add a using clause for the Microsoft.Exchange.WebServices.Data and Microsoft.Exchange.WebServices.Autodiscover namespaces. For more information, see How to: Add or Remove References in Visual Studio.

Add a service reference

To consume an external Web service from an Exchange server, you must first create a reference to the Web service. After creating a reference to the Web service, you can invoke the methods of the Web service from your application and identify the available methods by using IntelliSense. For more information, see How to: Add a Service Reference. A service reference to the Web service is not required if you are using the managed API.

Gather server information

To develop against Exchange Online, you need to know the name of the server and the server's domain name, and you must have a Windows Live ID. If you do not have this information, contact your system administrator. In addition, the Autodiscover is a means of finding the server name and URL.

Write the code

To use the EWS Managed API to write an application that creates and sends email, perform the following steps:

  1. Add reference namespaces.

    The .NET Framework classes are organized into namespaces. The Microsoft.Exchange.WebServices assembly, which is contained in Microsoft.Exchange.WebServices.dll, holds two namespaces: Microsoft.Exchange.WebServices.Data and Microsoft.Exchange.WebServices.Autodiscover. Include these namespaces in your application, as shown in the following example.

    using System;
    using Microsoft.Exchange.WebServices.Data;
    using Microsoft.Exchange.WebServices.Autodiscover;
    
    
  2. Create the service binding.

    To access EWS by using the EWS Managed API, all you need is an instance of the ExchangeService class. The ExchangeService object contains the Web methods on the Exchange Online server that receives the requests and returns the responses.

                ExchangeService service = GetBinding();
    
    
  3. Define credentials and set the service URL.

    To create the binding, set the service credentials. You set the service credentials by using your Windows Live ID and password, which were provided when you were added to the Exchange Online domain. For more information, see ExchangeService Class.

    Calling the AutodiscoverUrl method locates the service endpoints and sets the service URL. Although the service URL can be set explicitly, we recommend that you use the AutodiscoverUrl method to find and set the URL. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server.

    The AutodiscoverRedirectionUrlValidationCallback method is used to validate the redirection URL.

    Gg591267.note(en-us,EXCHG.140).gifNote:

    In an Exchange Online application, the AutodiscoverUrl method requires a validation callback. If you try to use the overloaded AutodiscoverUrl method without an AutodiscoverRedirectionUrlValidationCallback method, it will result in an error. To enable Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) method overload.

            static ExchangeService GetBinding()
            {
                // Create the binding.
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
    
                // Define credentials.
                service.Credentials = new WebCredentials("User1@ContosoDomain.onmicrosoft.com", "pass@word1");
    
                // Use the AutodiscoverUrl method to locate the service endpoint.
                try
                {
                    service.AutodiscoverUrl("User1@ContosoDomain.onmicrosoft.com", RedirectionUrlValidationCallback); 
                }
                catch (AutodiscoverRemoteException ex)
                {
                    Console.WriteLine("Exception thrown: " + ex.Error.Message);
                }
            }
    
  4. Create the redirection callback to validate the redirection URL.

            static bool RedirectionUrlValidationCallback(String redirectionUrl)
            {
                // Perform validation.
                // Validation is developer dependent to ensure a safe redirect.
                return true;
            }
    
  5. Create an EmailMessage item that will hold the information that describes the email message that is to be created in the Exchange store.

                EmailMessage message = new EmailMessage(service)
    
    
  6. Set the properties that describe the message. For information about the properties that can be set on an email message, see EmailMessage Properties. In this example, I set the Body, Subject, and ToRecipients properties.

                // Subject
                message.Subject = "Very Interesting. EWS Message";
    
                // Body
                message.Body = new MessageBody("This is a message, built using EWS and posted to an Exchange Online account");
    
                // Recipients
                message.ToRecipients.Add("User1@ContosoDomain.onmicrosoft.com");
                message.ToRecipients.Add("User2@ContosoDomain.onmicrosoft.com");
    
    
  7. Use the SendandSaveCopy method to send the mail to the computer that is running EWS and to save a copy of it in the Sent Items folder.

                // Send the mail. This makes a trip to the EWS server.
                message.SendAndSaveCopy();
    
    

Check the application

Use the application that you just created to create and send mail. You can use Microsoft Office Outlook, Microsoft Office Outlook Web App, your own application, or any other application to see if the mail has been sent to the server and delivered to a client.

Code example

The following example shows you how to create and send email from and Exchange Online account by using the EWS Managed API. This example uses the AutodiscoverUrl method to locate the EWS endpoint for the server, sets the service URL, creates a binding, creates an email message that has two recipients, and sends the mail to the EWS server.

using System;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;

namespace EWS_Exchange_Online_CS
{
    class Program
    {
        static void Main()
        {
            // Create a service binding and reuse it for each EWS call.
            ExchangeService service = GetBinding();

            // Call the CreateAndSendMail method.
            CreateAndSendMail(service);

            // Pause the console.
            ConsoleKeyInfo cki;
            Console.WriteLine("\n\nPress E to exit");
            while (true)
            {
                cki = Console.ReadKey(true);
                {
                    if (cki.Key == ConsoleKey.E) break;
                }
            }
        }

        static ExchangeService GetBinding()
        {
            // Create the binding.
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

            // Define credentials.
            service.Credentials = new WebCredentials("User1@ContosoDomain.onmicrosoft.com", "password");

            // Use the AutodiscoverUrl method to locate the service endpoint.
            try
            {
                service.AutodiscoverUrl("User1@ContosoDomain.onmicrosoft.com", RedirectionUrlValidationCallback); 
            }
            catch (AutodiscoverRemoteException ex)
            {
                Console.WriteLine("Exception thrown: " + ex.Error.Message);
            }

            // Display the service URL.
            Console.WriteLine("AutodiscoverURL: " + service.Url);
            return service;
        }

        // Create the callback to validate the redirection URL.
        static bool RedirectionUrlValidationCallback(String redirectionUrl)
        {
            // Perform validation.
            // Validation is developer dependent to ensure a safe redirect.
            return true;
        }

        // Create and send mail.
        static void CreateAndSendMail(ExchangeService service)
        {
            // Create an email message and identify the Exchange service.
            EmailMessage message = new EmailMessage(service);

            // Subject
            message.Subject = "Very Interesting. EWS Message";

            // Body
            message.Body = new MessageBody("This is a message, built using EWS and posted to an Exchange Online account");

            // Recipients
            message.ToRecipients.Add("User1@ContosoDomain.onmicrosoft.com");
            message.ToRecipients.Add("User2@ContosoDomain.onmicrosoft.com");

            // Send the mail. This makes a trip to the EWS server.
            message.SendAndSaveCopy();
        }
    }
}

Related topics

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.