Sending Email from Azure using Exchange Online

 

One of the major issue in having a LOB hosted in Azure was the that Azure does not have a SMTP server available on the cloud.

You would be used to using System.Web.Mail from your application to send mails but this requires a SMTP server the application can connect to. This is the part which is not available in Azure. But the ship is still afloat!!

You could do one of there:

  • Talk to 3rd party providers who can help sending emails
  • Connect to you on-premise SMPT servers / Email Forwarders
  • Use APIs / Services exposed by mail servers to send mails.

The 3rd approach could possibly be the best way to implement this as you can leverage Exchange Online services. https://www.microsoft.com/online/exchange-online.aspx claims, all you need to pay is $5 per user per month and considering the minimum you go for is a 5 user package, you are still paying 25 $ a month which works out pretty economical considering the advantages you get like:

  • Improved e-mail security
  • "From virtually-anywhere" access to e-mail for your employees
  • Enhanced operational efficiency for your IT staff
  • 25-gigabyte (GB) mailbox storage per standard license

Exchange online also exposes web services which you could use within your Azure application to send emails.

Here is what you need to do to get things going.

Get a Trial Subscription for Exchange Online. Create user accounts and remember the username, password.

Next you need to connect to Exchange Webservices. how do you the address of that? here they are:

APAC https://red003.mail.apac.microsoftonline.com/ews/Services.wsdl
EMEA https://red002.mail.emea.microsoftonline.com/ews/Services.wsdl
NA https://red001.mail.microsoftonline.com/ews/Services.wsdl">https://red001.mail.microsoftonline.com/ews/Services.wsdl

Add to service reference to your Azure project and you are all ready to use the APIs to Read/Write/Monitor mails and mailboxes.

Wondering what the code should be? Here you go:

                 var Username = RoleEnvironment.GetConfigurationSettingValue("EWSUserName");
                var Password = RoleEnvironment.GetConfigurationSettingValue("EWSPassword");

                var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.Url = new Uri("https://red001.mail.microsoftonline.com/ews/exchange.asmx");
                service.Credentials = new WebCredentials(Username, Password);

                var message = new EmailMessage(service);
                message.ToRecipients.Add(txtTo.Text);
                message.From = new EmailAddress(txtFrom.Text);
                message.Subject = txtSubject.Text;
                message.Body = new MessageBody(BodyType.HTML, txtBody.Text);

                message.Send();

                lbStatus.Text = "Mail send sucessfuly to " + txtTo.Text + "..";

Here is the sample I had created to test this.

Exchange Online Azure Integration

Happy Coding !!