Sending email from a windows azure application using an Office 365 mailbox

For one of my projects at work I’m trying to figure out what the best way of sending email from a Windows Azure application is.

Installing your own SMTP server in the cloud is not recommended for a couple of reasons. The main one, not having a static IP, which is a requirement when configuring the SMTP server to its DNS and PTR records.

So that leaves me with a few other choices:

  1. Use an on-premise SMTP server
  2. Use a third party email provider
  3. Use our new Office 365 to relay mail

Well option 1. isn’t going to be sustainable for the long run seeing as our on-premise Exchange server is being replaced by Exchange Online which is part of the Office 365 offering.

Option 2. Is a very good option and probably the best on which I will elaborate shortly.

For my setup I chose option 3, for the most part because I liked the idea of having a full Microsoft solution.

So what exactly did I do? I created myself a MVC.Net 3 application that includes the following class:

/// <summary>
/// Sends email
/// </summary>
public class EmailNotifier
{
    readonly string smtpUserName;
    readonly string smtpPassword;
    readonly string fromEmailAddress;
    readonly string smtpServer;
    readonly string smtpPort;

    public EmailNotifier()
    {
        smtpUserName = RoleEnvironment.GetConfigurationSettingValue("SmtpUsername");
        smtpPassword = RoleEnvironment.GetConfigurationSettingValue("SmtpPassword");
        smtpServer = RoleEnvironment.GetConfigurationSettingValue("SmtpServer");
        smtpPort = RoleEnvironment.GetConfigurationSettingValue("SmtpPort");
        fromEmailAddress = RoleEnvironment.GetConfigurationSettingValue("EmailAddress");
    }

    /// <summary>
    /// Sends an email to specfied address <paramref name="emailAddress"/>.
    /// </summary>
    /// <param name="emailAddress">The email address.</param>
    /// <param name="subject">The subject.</param>
    /// <param name="body">The body.</param>
    public void SendEmail(string emailAddress, string subject, string body)
    {
        var message = new MailMessage(fromEmailAddress, emailAddress)
        {
            Subject = subject,
            Body = body,
            IsBodyHtml = false,
            BodyEncoding = Encoding.UTF8
        };
        
        var smtpClient = new SmtpClient(smtpServer, int.Parse(smtpPort))
        {
            Credentials = new NetworkCredential(smtpUserName, smtpPassword),
            EnableSsl = true
        };
            
        smtpClient.Send(message);
    }
}

As you can see nothing special just a simple class using the System.Net namespace to send email directly via the SmtpClient class. Just note line 40 using SSL is required.

As for the SMTPserver and Port settings we need to retrieve those from Office 365. If you login to the account, from which name you will be sending the email from, go to the account settings page and at the bottom you’ll find the Settings for POP, IMAP and SMTP Access.

image

So nothing to it! But wait there is a little caveat I should warn about. Unlike your on-premise SMTP server, Office 365 has some limits as to the amount of recipients a single user can send emails to. These limits can be found in the Office 365 help. So I have a limit of 500 emails recipients I can send email to, good enough for me.

If your requirement is to send lots and lots of email this might not be the best solution and that’s why I would recommend Option 2 too use a third-party email provider. (ie. unifiedemail, sendgrid)

Advertisement

10 thoughts on “Sending email from a windows azure application using an Office 365 mailbox

  1. Hello, this code doesnt appear to be working for me. I get the following error:

    {“The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated”}

    In doing some research I found out that the SmtpClient class does not support TLS which is needed for Office 365. Has this code been working for you?

    1. Hi Param,

      Yes I created and tested it.

      If I get the time, I’ll test it again it’s been awhile maybe some settings changed in office365.

      Please make sure your credentials are good because if they are not you will get the same error.

      Cheers,

      Hans

      1. Hello Hans,

        I have the same problem as Param. If you can let us know if you can execute the same code today, then that may validate our own issues.

        The issue I am having is that I can’t use a different email address, as the message from address, than the account used to authenticate the SMTP call.

        Any ideas?

        Paul

      2. Hi Paul,

        I can still use the same code as posted. I use these settings:

        I just ran it locally, aswell as on azure, both work.

        Hope this helps

        Hans

      1. Setting name=”SmtpUsername” value=”hawa@avsol.onmicrosoft.com”
        Setting name=”SmtpPassword” value=”notmypassword”
        Setting name=”SmtpServer” value=”pod51017.outlook.com”
        Setting name=”SmtpPort” value=”587″
        Setting name=”EmailAddress” value=”hawa@avsol.onmicrosoft.com”

Leave a Reply to Paul Patterson Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s