Tuesday, 15 October 2019

Sending Emails Using the Office 365 APIs


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.

Sending E-mail Using 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.
  1. ExchangeService myservice = new
  2. ExchangeService(ExchangeVersion.Exchange2010_SP1);
  3. myservice.Credentials = new
  4. WebCredentials(ConfigurationSettings.AppSettings
  5. .Get("SenderEmailid").ToString(),
  6. ConfigurationSettings.AppSettings.Get("Password").ToString());
  7.  
  8. try
  9. {
  10. string serviceUrl =
  11. ConfigurationSettings.AppSettings
  12. .Get("Office365WebserivceURL").ToString();
  13. myservice.Url = new Uri(serviceUrl);
  14. EmailMessage emailMessage = new EmailMessage(myservice);
  15. emailMessage.Subject = "Test Subject";
  16. emailMessage.Body = new MessageBody("Testing Exchange Web
  17. Service API");
  18. emailMessage.ToRecipients.Add(ConfigurationSettings
  19. .AppSettings.Get("Recipients").ToString());
  20. emailMessage.Send();
  21. }
  22. catch (SmtpException exception)
  23. {
  24. string msg = "Mail cannot be sent (SmtpException):";
  25. msg += exception.Message;
  26. throw new Exception(msg);
  27. }
  28.  
  29. catch (AutodiscoverRemoteException exception)
  30. {
  31. string msg = "Mail cannot be
  32. sent(AutodiscoverRemoteException):";
  33. msg += exception.Message;
  34. throw new Exception(msg);
  35.  
  36. }

Sending E-mail Using Office 365

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.
  1. MailMessage = new MailMessage();
  2. mailMessage.From = new
  3. MailAddress(ConfigurationSettings.AppSettings
  4. .Get("SenderEmailid").ToString());
  5. mailMessage.To.Add(new
  6. MailAddress(ConfigurationSettings.AppSettings
  7. .Get("Recipients").ToString()));
  8. mailMessage.Subject = "Test Subject";
  9. mailMessage.Body = "Testing Office365 Email";
  10. mailMessage.IsBodyHtml = true;
  11. SmtpClient client = new SmtpClient();
  12. client.Credentials = new NetworkCredential("", "");
  13. client.Port = 587;
  14. client.Host = "smtp.office365.com";
  15. client.EnableSsl = true;
  16. client.send(mailmessage);
The following app settings entries are used in both the preceding examples:
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <appSettings>
  4. <add key="SenderEmailid" value="user" />
  5. <add key="Password" value="password" />
  6. <add key="Office365WebserivceURL"
  7. value="https://outlook.office365.com/ews
  8. /exchange.asmx" />
  9. <add key="Recipients" value="samplemail@gmail.com" />
  10. </appSettings>
  11. </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.

No comments:

Post a Comment

Hide New... button on lookup controls in model-driven apps

  The 'New ...' button is shown upon opening the lookup search dialog whenever the logged in user has at least user create privileg...