SendGrid is one of the most popular services for delivering the email messages. It allows sending both, the regular, plain text email messages as well as the templated ones (called “transactional”). On top of that, it has also some other cool features such as marketing templates. In this post, I’m going to describe the way that the Warden project has been integrated with this service.
As every other plugin, the SendGrid integration is a available as a NuGet package:
Install-Package Warden.Integrations.SendGrid -Pre
So what it does? As the name states, it integrates the Warden instance with the SendGrid service in order to easily send the email messages e.g. about some resource failure that has just become unavailable. The configuration per se is pretty straightforward, just take a look:
var wardenConfiguration = WardenConfiguration
.IntegrateWithSendGrid("api-key", "firstname.lastname@example.org", cfg =>
.SetGlobalWatcherHooks((hooks, integrations) =>
hooks.OnStart(check => GlobalHookOnStart(check))
.OnFailure(result => integrations.SendGrid().SendEmailAsync("Monitoring errors have occured."))
//Configure watchers, hooks etc..
All you need to do, is to call the the available extension method IntegrateWithSendGrid(), where you can either pass the API key or credentials (username and password).
Once it’s done, you can use another extension method available via the integration, which is simply called SendEmailAsync().
Actually, it’s more than just this single method – there are available its overloads as well as the another function name SendTemplatedEmailAsync() which allows to make use of the transactional templates.
There are also additional options that you can use to reduce the amount of the same code while sending emails e.g. WithDefaultTemplateId(), WithHtmlBody() or even the WithEmailSenderProvider() if you would like to control the way that the email messages are being sent.
I’ve implemented this functionality to make it simpler to get notified whenever something wrong happens to the resources being monitored by the Warden.
Given the fact that the SendGrid can be used for free (10k messages per month), I do not find any reason not to try it out ;).