Introduction
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.
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.
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 ExchangeService(ExchangeVersion.Exchange2010_SP1); myservice.Credentials = new WebCredentials(ConfigurationSettings.AppSettings .Get("SenderEmailid").ToString(), ConfigurationSettings.AppSettings.Get("Password").ToString()); try { string serviceUrl = ConfigurationSettings.AppSettings .Get("Office365WebserivceURL").ToString(); myservice.Url = new Uri(serviceUrl); EmailMessage emailMessage = new EmailMessage(myservice); emailMessage.Subject = "Test Subject"; emailMessage.Body = new MessageBody("Testing Exchange Web Service API"); emailMessage.ToRecipients.Add(ConfigurationSettings .AppSettings.Get("Recipients").ToString()); emailMessage.Send(); } 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 sent(AutodiscoverRemoteException):"; 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 MailAddress(ConfigurationSettings.AppSettings .Get("SenderEmailid").ToString()); mailMessage.To.Add(new MailAddress(ConfigurationSettings.AppSettings .Get("Recipients").ToString())); 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 = "smtp.office365.com"; client.EnableSsl = true; client.send(mailmessage);
The following app settings entries are used in both the preceding examples:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="SenderEmailid" value="user" /> <add key="Password" value="password" /> <add key="Office365WebserivceURL" value="https://outlook.office365.com/ews /exchange.asmx" /> <add key="Recipients" value="samplemail@gmail.com" /> </appSettings> </configuration>
API Call Limitations
All Outlook APIs accessed via https://outlook.office.com/api or https://outlook.office365.com/api 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.
Summary
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.