Press enter to see results or esc to cancel.

Send Mails using Azure Function Apps

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePin on PinterestShare on Tumblr

To send mails using Azure Function Apps is actually a case where I can see where Azure Function Apps really shine, not only when it comes to building an architecture of an app-network, but also when it comes to paying for services.

Microsoft announced on March 31st 2016 the release of Azure Function Apps – a new member of the Web & Mobile family in Azure.

Azure Functions is an event driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in Azure or third party service as well as on-premises systems.

In this blog post I will describe how you can create an Azure Function App that sends emails for you. The purpose of this application is that all App Service applications that have some sort of sending email function will use the Azure Function App instead.

The up-sides of this is of course that you only need to implement the logic for sending the email once, instead of implementing it in every application you have deployed.
The seconds up side is the configuration. Imagine you would change your SMTP server credentials. These credentials would tradionally be needed to be updated in all your app service applications. By using a single function app – you only need to update the configuration once and therefore reducing the cost and time that is needed for this step.

Create an Azure Function App

01 - create function app
Image 01 – create function app

Image 01 simply shows where to find the Azure Function App in your “new Azure artifact” blades, this of course is not enough for you to create the app. By clicking on the Function App icon, a new blade will open (shown in Image 02) in which you provide the app name and the basic settings needed for most of the app service applications, except for one thing – Azure Function Apps require you to set up a storage account.

02 - set up function app
Image 02 – Set up function app

In my demo case, I will call this function app – rwlmayerdemo01mail and will call all other required settings accordingly.

When pressing the create button – your function app will be deployed to Azure and be active within just a couple of seconds (sometimes minutes).

When the Azure function app is deployed it is accessible either on your Azure Portal Dashboard or under “All Resources” in your menu blade to your left.

Start Coding your Function

When clicking on your Function App you have the opportunity to create a function app with different kind of templates (see picture 01). These templates are different templates based on the way you want your Function App to be triggered (see image 03).

03 - get started with your azure function app
Image 03 – Get started with your Azure Function App

Azure Function Apps can be triggered by:

  • Timers – using CRON expressions
  • Changes to your Azure Blob Containers
  • Azure Queues
  • Service Bus messaging
  • HTTP triggers

In this case we will be using “Create a function from scratch” (which can be found on the bottom of the screen under the “Or get started on your own” header) in which we want our function to be called by an HTTP. By clicking the “Create a function from scratch” you will face a new page in which a new set of templates is shown – these templates are written in C# and NodeJS.

Azure Function Apps can be written in

  • C# .NET
  • NodeJS
  • Python
  • PHP

In this demo, since I want it to be trigger by an HTTP call and I am most familiar with C#.NET I am going to choose the “Generic Webhook – C#” as shown in Image 04 below and call it sendmail.

04 - http triggered function app
Image 04 – Generic Webhook – C#

After you press click, the function is created and the fun begins! You will face a new page – showing you how you develop your Azure Function App (see image 05 below).

05 - editing Azure Function App
Image 05 – Editing Azure Function App

I have marked with image with 4 number annotations

  1. Function Url: This is the URL which is used in ord your newly deployed Function App to be triggered.
  2. Code – The code that is executed when the Function App is triggered.
  3. Logs – This is similar to any other log tools – this log shows compiling information, trigger information and so on, very useful! (let me know if you find out how to listen to this log via API calls).
  4. Run – Allows you to test your HTTP call. The run section will send an HTTP post to the Function Url with the defined Request body.

The code being showed in the Code window is basic C# .NET ASP .NET code, which I will not explain, since I recommend you to get a ground knowledge for ASP.NET and ASP.NET MVC.
But in a short summary, what this code does is retrieving an HTTP request and returning an HTTP response. The HTTP response can be formed just the way you want with an HTTP status, headers and a response body – the sky is the limit.

I would recommend you to do some tests, change the code, execute it using the Run window and so on to get a feeling for how this works.

Sending Emails

In my scenario, I would like my Azure Function App to retrieve the following parameters as part of the request body.

  1. from Email – from which the mail is sent
  2. to Email – the receiver of the email
  3. subject – the subject
  4. message – the email content
  5. is important – whether the priority should be high

And since I want the function app to use Json format, an example of the request body would look like this:

The above request body is just an example how I want my demo to work, if you want to extend or reduce the number of attributes please do so.

The C# .NET code for this example would look like this:

In which I pretty much just declare a bunch of variables needed for the SmtpClient and MailMessage to work. We now need to fix the provided code by the Azure Function App, with the code from the above GIST I wrote.

The code should basically work right away, the only thing we need to do is to retrieve the parameters via the request body and put it into the in the code early declared variables. (see the below GIST)

For this to work, please check row number 23 to 25 and fill in your SMTP server information and put it into the code part of the Azure Function view (shown in image 05) and press save. When saving the provided code will automatically be compiled and the output will be logged to the Log window shown in image 05. The output log will show the compilation info, whether the compilation failed or not.

When you are done, what you can do is go to the Run part of the Azure Function view shown in Image 05 and put in your example request body and press “Run” and the result should look like image 06 below.

Image 06 - Send email using run test in Azure Portal
Image 06 – Send email using run test in Azure Portal

Within just a couple of seconds, depending on which SMTP relay / provider you are using, you should receive an email being sent from your Azure Function App!


Azure Functions Apps are pretty useful and a great new member of the app service family (calling it part of the app service family, since it uses app service plans. Microsoft did not put it under the App Service on

There are quite some business cases out there where I can see Azure Functions to be in production and of course… do not forget the great pricing model Microsoft actually offers. See Microsoft pricing text below:

With Azure Functions you only pay for what you use with compute metered to the nearest 100ms at Per/GB price based on the time your function runs and the memory size of the function space you choose. Function space size can range from 128mb to 1536mb. With the first 400k GB/Sec free.

I hope this blog post has been useful and I will cover more of Azure Function Apps in the future – stay tuned 🙂

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePin on PinterestShare on Tumblr

1 Comment

Eric Barb

Very cool. Just set this up myself to address some needs. I wish I had thought of and found this sooner.

Leave a Comment