Sending Emails Using the Office 365 APIs

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


Developers now can send e-mails using Exchange Online as an SMTP server configured in the configuration file. Office 365 APIs also provide options to developers for sending e-mails from devices/applications. Microsoft Office 365 API Tools for Visual Studio enable developers to integrate Office 365 services into their applications. Office 365 APIs can be accessed by using libraries available for server-side .NET, client-side JavaScript, and SDKs. These libraries help developers to interact with the REST APIs from the device or platform of the developer’s choice. To access these Office 365 APIs, developers have to register their apps with Microsoft Windows Azure AD.

In this article, I will create sample C# applications to demonstrate send e-mail functionality using Office 365 and Exchange Web Services APIs. If you find yourself struggling with C# or want to improve your knowledge, consider visiting the TechRepublic Academy!

Sending E-mail Using C# and the Exchange Webs Services API

The Exchange Web Services (EWS) API provides abstractions of XML messages, XML serialization of the HTTP requests, and responses that are sent between the client and server. By using the Exchange Web Services, developers now can control an Exchange mailbox from code. The following C# console application will demonstrate the send e-mail functionality using EWS.

Step 1

Create a new Visual Studio Console application and name it Email Client.

Create a new Console application project
Figure 1: Create a new Console application project

Step 2

Add the Microsoft.Exchange.Webservices NuGet package reference to your console application project. Microsoft Exchange Web Services (EWS) is an interface to programmatically manage Exchange items such as calendar, contacts, and e-mail.

Add the Microsoft.Exchange.Webservices NuGet package
Figure 2: Add the Microsoft.Exchange.Webservices NuGet package

Step 3

Write the following code in the Main() method for send e-mail functionality.

ExchangeService myservice = new
myservice.Credentials = new

   string serviceUrl =
   myservice.Url = new Uri(serviceUrl);
   EmailMessage emailMessage = new EmailMessage(myservice);
   emailMessage.Subject = "Test Subject";
   emailMessage.Body = new MessageBody("Testing Exchange Web
      Service API");
catch (SmtpException exception)
   string msg = "Mail cannot be sent (SmtpException):";
   msg += exception.Message;
   throw new Exception(msg);

catch (AutodiscoverRemoteException exception)
   string msg = "Mail cannot be
   msg += exception.Message;
   throw new Exception(msg);


Sending E-mail Using Office 365 and C#

Sending e-mail using Office 365 API is easy; developers just have to use the build feature in the SMTP client and the Mailmessage objects of .NET. You need to specify the host name, port, EnableSSL, and the credentials properties of the SmtpClient object. Both 587 and 25 ports are supported for SMTP operation; however, 587 is the recommended port. Both the username and password could be kept in a configuration file or in a database, in encrypted format. In the following code example, I have kept it in the app.config file.

MailMessage = new MailMessage();
mailMessage.From = new
mailMessage.Subject = "Test Subject";
mailMessage.Body = "Testing Office365 Email";
mailMessage.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("", "");
client.Port = 587;
client.Host = "";
client.EnableSsl = true;

The following app settings entries are used in both the preceding examples:

<?xml version="1.0" encoding="utf-8" ?>
      <add key="SenderEmailid" value="user" />
      <add key="Password" value="password" />
      <add key="Office365WebserivceURL"
                  /exchange.asmx" />
      <add key="Recipients" value="" />

API Call Limitations

All Outlook APIs accessed via or have a limit is 60 requests per minute, per user (or group), per app ID. So, for now, developers only can make limited APIs calls from the app. Read through the Microsoft Blog Post for more details on REST API call limitations.


I hope that you have learned how to access the Office 365 REST APIs and send e-mails by reading this article. Stay tuned and wait for my next article.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read