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 ability 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 using no longer seems valid.
As I needed to use this feature I spent some time digging into getting this working with the GA release of Azure Functions (version ~1). Thankfully as Functions is an abstraction over WebJobs I had plenty of information on how to do it right now thanks to the WebJobs documentation and extensibility :).
Here’s how you can get this working too:
1. Register your SendGrid API key in Application Settings: you must utilise the documented approach of setting your API key in an App Setting called “AzureWebJobsSendGridApiKey”. Without this your Function won’t be able to send mail successfully.
2. Import the SendGrid nuget package into your Function by creating a project.json file that contains this following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"frameworks": { | |
"net46": { | |
"dependencies": { | |
"Sendgrid": "8.0.5" | |
} | |
} | |
} | |
} |
3. Create an output binding on your function that will allow you send the message without needing to create client code in your Function:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"bindings": [ | |
{ | |
"type": "sendGrid", | |
"name": "message", | |
"direction": "out", | |
"from": "[email protected]", | |
"subject": "Functions r0ck5!" | |
} | |
], | |
"disabled": false | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Microsoft.Azure.WebJobs.Extensions.SendGrid" | |
using SendGrid.Helpers.Mail; | |
public static void run(out Mail message) | |
{ | |
// see the associted function.json & projcet.json files for how to get this working. | |
// function.json: https://gist.github.com/sjwaight/1394817ab84f8b6688a11be8621364fb | |
// project.json: https://gist.github.com/sjwaight/0c2392236b56ef914f0b4b10b786a8fb | |
// NOTE: no need to set the 'sender/from' property as the output binding does that for us. | |
var personalization = new Personalization(); | |
personalization.AddTo(new Email("[email protected]")); | |
var messageContent = new Content("text/html", "My fantastic mail body contents here."); | |
message = new Mail(); | |
message.AddContent(messageContent); | |
message.AddPersonalization(personalization); | |
} |
If you want to do a lot more customisation of the email that is sent you can simply refer to the SendGrid C# Library on Github which covers features such as sending using templates.
Once the Functions team publishes an updated approach to using SendGrid I’ll make sure to link to it from here. In the meantime… happy mailing!
Reblogged this on Kloud Blog.
So I want to put the mail in private method in my function, but I keep getting the error.
Exception while executing function: Functions.XXXXXXXXXXXXX. Microsoft.Azure.WebJobs.Host: Error while handling parameter message after function returned:. Microsoft.Azure.WebJobs.Extensions.SendGrid: A ‘To’ address must be specified for the message
I think the problem is how I’m passing my ‘out Mail message’ to the method.
I think it might be easier not to use the binding.
Dan, yes in certain circumstances the use of ‘out’ parameters isn’t going to work. You can use imperative binding to add bindings at run time if that makes more sense, or simply call native client code. I’ve personally not used the imperative model and have tended to use client-code which has worked well for me.
So I want to put the mail in private method in my function, but I keep getting the error.
Hi there… Without seeing your code it’s hard to say why you are getting an error response, however, you do need to be mindful of the output binding to SendGrid which requires you populate the necessary Mail object before the Function exits.