Thank you!
We just passed 6000 of you really wonderful people who follow me here and send content. I try to ensure that each newsletter issue is of sufficient quality to provide you with enough value.
I would like you to write me testimonials that would help me and other people who would come to the Newsletter. At the end of the completed testimonials, as a sign of gratitude, I will give you something valuable.
Thank you!
Background
New user registered on your app?
The verification code should be sent to his email.
User forgot password?
You should send him a password reset link to his email.
And a thousand more such examples. Production applications today cannot live without some kind of email sending. That's why we need something easily and quickly configurable for sending emails.
Introducing FluentEmail.
FluentEmail is a popular open-source library for sending emails from .NET applications. It provides an fluent interface (fluent syntax). That means we can easily create an email message, add recipients, set the subject, etc. by chaining methods.
Let's dive in.
Quick Configuration
The first thing that is necessary is to add a new package from the NuGet package manager.
dotnet add package FluentEmail.Core
dotnet add package FluentEmail.Smtp
Now you can use the library.
How?
Basic Usage
To construct and send an email you just keep building up the fluent methods on the IEmail class (comes from FluentEmail). When you are ready to send, call SendAsync ().
var email = await Email
.From("stefan@gmail.com")
.To("milan@gmail.com", "Milan")
.Subject("Hi Milan!")
.Body("Works!")
.SendAsync();
There are the most common methods available on the email object:
• .To (string emailAddress) - add recipients
• .SetFrom (string emailAddress) - change the sender address.
• .CC/BCC (string emailAddress) - add CC or BCC
• .Subject (string subject) - set the subject
• .Body (string body) - set the message body (without templating)
• .Attach (Attachment attachment) - add attachments
Better way?
Dependency Injection
builder.Services.
.AddFluentEmail("from@gmail.com")
.AddRazorRenderer()
.AddSmtpSender("localhost", 25);
What I did here?
• .AddFluentEmail () - sets up FluentEmail with a default SendFrom address.
• AddRazorRenderer () - sets the RazorRenderer provider - templating support (ITemplateRenderer interface).
public class EmailService : IEmailService
{
private readonly IFluentEmail _fluentEmail;
public EmailService(IFluentEmail fluentEmail)
{
_fluentEmail = fluentEmail;
}
public async Task Send()
{
await _fluentEmail
.To("test@gmail.com")
.Subject("Test email")
.Body("Test body")
.SendAsync();
}
}
Razor Email Templates
We want the emails we send to have the same template and style. Especially if we send a newsletter, like this one for example :D
One of the most popular options for template rendering is using the Razor template renderer . To use this renderer, it is necessary to add the FluentEmail.Razor package from NuGet.
dotnet add package FluentEmail.Razor
The RazorRenderer supports any valid Razor code .
Let's see the example:
//configure the Razor Renderer
builder.Services
.AddFluentEmail("from@gmail.com")
//pass in a type in the assemble with embedded tempaltes
.AddRazorRenderer(typeof(Program))
//In your template code include a layout file
//the template could be sourced from file/embedded if that is configured
var template = @"
@{Layout = ""./Shared/_Layout.cshtml""; }
Hi @Model.Name here is a list @foreach(var i in Model.Numbers) { @i }";
var model = new { Name = "Stefan", Numbers = new [] { 1, 2, 3} };
var email = new Email()
.To("test@gmail.com")
.Subject("Razor template example")
.UsingTemplate(template, model);
Some other templates?
There is a support for Liquid Templates , also. The Liquid Templates are a more restricted templating language created by Shopify. They are more lightweight than Razor templates as well as safer and simpler for end users to create. Properties on the model are made available as Liquid properties in the template.
Email Senders
FluentEmail allows you to plug in popular email sending providers (or build your own by implementing ISender ).
The following senders are available as core libraries:
- SMTP Sender
- Mailgun Sender
- SendGrid Sender
- MimeKit Sender
First, you need to add another package from the NuGet:
dotnet add package FluentEmail.SendGrid
builder.Services
.AddFluentEmail("from@gmail.com")
.AddSendGridSender("apikey");
And that's it. You have integrated it.
This is so perfect!
What next?
I showed you how with the help of the FluentEmail library it is possible to send an email in 5 minutes. It can be easily integrated with various Email Senders, as well as being able to include various renders.
That's all from me today.