This library is based on the excellent FluentEmail by Luke Lowrey, which, at the time I'm writing this, looks like it is not being maintained any longer (unfortunately).
Note: This library is not compatible with .NET Framework projects.
- FluentEmailer.Core - Just the domain model. Includes very basic defaults, but is also included with every other package here.
- FluentEmailer.Smtp - Send email via SMTP server.
- FluentEmailer.Razor - Generate emails using Razor templates. Anything you can do in ASP.NET is possible here. Uses the RazorLight project under the hood.
- FluentEmailer.Liquid - Generate emails using Liquid templates. Uses the Fluid project under the hood.
- FluentEmailer.MailerSend - Send email via MailerSend's REST API.
- FluentEmailer.Mailgun - Send emails via Mailgun's REST API.
- FluentEmailer.Mailtrap - Send emails to Mailtrap. Uses FluentEmailer.Smtp for delivery.
- FluentEmailer.MailKit - Send emails using the MailKit email library.
- FluentEmailer.SendGrid - Send email via SendGrid's REST API.
var email = await Email
.From("john@email.com")
.To("bob@email.com", "bob")
.Subject("hows it going bob")
.Body("yo bob, long time no see!")
.SendAsync();
Configure FluentEmailer in startup.cs with these helper methods. This will inject IFluentEmailer
(send a single email) and IFluentEmailerFactory
(used to send multiple emails in a single context) with the
ISender and ITemplateRenderer configured using AddLiquidRenderer(), AddSmtpSender() or other packages.
public void ConfigureServices(IServiceCollection services)
{
services
.AddFluentEmail("fromemail@test.test")
.AddRazorRenderer()
.AddSmtpSender("localhost", 25);
}
Example to take a dependency on IFluentEmailer:
public class EmailService {
private IFluentEmailer _fluentEmailer;
public EmailService(IFluentEmailer fluentEmailer) {
_fluentEmailer = fluentEmailer;
}
public async Task Send() {
await _fluentEmailer.To("hellO@gmail.com")
.Body("The body").SendAsync();
}
}
// Using Razor templating package (or set using AddRazorRenderer in services)
Email.DefaultRenderer = new RazorRenderer();
var template = "Dear @Model.Name, You are totally @Model.Compliment.";
var email = Email
.From("bob@hotmail.com")
.To("somedude@gmail.com")
.Subject("woo nuget")
.UsingTemplate(template, new { Name = "Luke", Compliment = "Awesome" });
Liquid templates are a more secure option for Razor templates as they run in more restricted environment. While Razor templates have access to whole power of CLR functionality like file access, they also are more insecure if templates come from untrusted source. Liquid templates also have the benefit of being faster to parse initially as they don't need heavy compilation step like Razor templates do.
Model properties are exposed directly as properties in Liquid templates so they also become more compact.
See Fluid samples for more examples.
// Using Liquid templating package (or set using AddLiquidRenderer in services)
// file provider is used to resolve layout files if they are in use
var fileProvider = new PhysicalFileProvider(Path.Combine(someRootPath, "EmailTemplates"));
var options = new LiquidRendererOptions
{
FileProvider = fileProvider
};
Email.DefaultRenderer = new LiquidRenderer(Options.Create(options));
// template which utilizes layout
var template = @"
{% layout '_layout.liquid' %}
Dear {{ Name }}, You are totally {{ Compliment }}.";
var email = Email
.From("bob@hotmail.com")
.To("somedude@gmail.com")
.Subject("woo nuget")
.UsingTemplate(template, new ViewModel { Name = "Luke", Compliment = "Awesome" });
// Using Smtp Sender package (or set using AddSmtpSender in services)
Email.DefaultSender = new SmtpSender();
//send normally
email.Send();
//send asynchronously
await email.SendAsync();
var email = Email
.From("bob@hotmail.com")
.To("somedude@gmail.com")
.Subject("woo nuget")
.UsingTemplateFromFile($"{Directory.GetCurrentDirectory()}/Mytemplate.cshtml", new { Name = "Rad Dude" });
var email = new Email("bob@hotmail.com")
.To("benwholikesbeer@twitter.com")
.Subject("Hey cool name!")
.UsingTemplateFromEmbedded("Example.Project.Namespace.template-name.cshtml",
new { Name = "Bob" },
TypeFromYourEmbeddedAssembly.GetType().GetTypeInfo().Assembly);