Prior to Azure Functions announcing their General Availability (GA) I had previously used SendGrid as an output binding in order to send email messages.
Since GA, however, the capacity to use SendGrid remains undocumented (I assume to give the Functions team time to test and document the binding properly) and the old approach I was spending no longer seems valid.
As I needed to use this article I consumed some time digging into getting this working with the GA release of Azure Functions (version ~1). Thankfully as Roles is an abstraction completed Web Jobs I had plenty of information on how to do it correct now thanks to the WebJobs documentation and extensibility :).
Here’s how you can get this working too:
1. List your SendGrid API key in Application Settings: you must utilise the documented method of setting your API key in an App Setting called “AzureWebJobsSendGridApiKey”. Without this your Role won’t be able to send mail positively.
2. Import the SendGrid nuget set into your Role by making a project.json file that contains this following:
3. Create an output binding on your function that will allow you send the message without needing to create client code in your Function:
"subject": "Functions r0ck5!"
4. Add a reference and using statement in your run.csx to ensure you have the right packages included. You can see this in the run.csx below that has all you need to create and send a simple email to a single recipient.
public static void run(out Mail message)
// see the associted function.json & projcet.json files for how to get this operational.
// function.json: https://gist.github.com/sjwaight/1394817ab84f8b6688a11be8621364fb
// project.json: https://gist.github.com/sjwaight/0c2392236b56ef914f0b4b10b786a8fb
// NOTE: no requirement to set the 'sender/from' property as the output binding does that for us.
var personalization = new Personalization();
var messageContent = fresh Content(“text/html”, “My fantastic mail body matters here.”);
message = new Mail();
If you want to do a lot extra customisation of the email that is sent you can basically refer to the SendGrid C# Library on Github which covers features such as sending using templates.
Once the Roles team publishes a simplified method to using SendGrid I’ll make sure to link to it from here. In the meantime… happy mailing!