The easiest way to send email from .NET and .NET Core. Use Razor or Liquid for email templates and send using SendGrid, MailGun, MailKit, SMTP and more.
Forked from original by @lukencode
My packages are the same names, but prefixed with jcamp.
to differentiate them.
The original repo has not been updated in almost a year and I needed some updates to the package that were provided by various PRs. I've tried to give all credit where due.
Original blog post here for a detailed guide A complete guide to send email in .NET
- FluentEmail.Core - Just the domain model. Includes very basic defaults, but is also included with every other package here.
- FluentEmail.Smtp - Send email via SMTP server.
- FluentEmail.Razor - Generate emails using Razor templates. Anything you can do in ASP.NET is possible here. Uses the RazorLight project under the hood.
- FluentEmail.Liquid - Generate emails using Liquid templates. Uses the Fluid project under the hood.
- FluentEmail.Bootstrap - Processes email templates after rendering through UnDotNet.BootstrapEmail to allow simpler templates to generate perfect emails.
- FluentEmail.Azure - Send emails via Azure Email Communication Services API
- FluentEmail.Postmark - Send emails via Postmark's REST API. Original Source/Credit
- FluentEmail.Mailgun - Send emails via MailGun's REST API.
- FluentEmail.SendGrid - Send email via the SendGrid API.
- FluentEmail.Mailtrap - Send emails to Mailtrap. Uses FluentEmail.Smtp for delivery.
- FluentEmail.MailKit - Send emails using the MailKit email library.
- FluentEmail.MailPace - Send emails via the MailPace REST API.
- FluentEmail.MailerSend - Send email via MailerSend's 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 FluentEmail in startup.cs with these helper methods. This will inject IFluentEmail (send a single email) and IFluentEmailFactory (used to send multiple emails in a single context) with the ISender and ITemplateRenderer configured using AddRazorRenderer(), AddSmtpSender() or other packages.
public void ConfigureServices(IServiceCollection services)
{
services
.AddFluentEmail("fromemail@test.test")
.AddRazorRenderer()
.AddSmtpSender("localhost", 25);
}
Example to take a dependency on IFluentEmail:
public class EmailService {
private IFluentEmail _fluentEmail;
public EmailService(IFluentEmail fluentEmail) {
_fluentEmail = fluentEmail;
}
public async Task Send() {
await _fluentEmail.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" });
There is a set of extensions in EmbeddedTemplates
that allows for use of embedded templates without specifying the assembly and the path every time.
EmbeddedTemplates.Configure(Assembly.GetCallingAssembly(), "FluentEmail.Core.Tests");
var email = Email
.From(fromEmail)
.To(toEmail)
.Subject(subject)
.UsingTemplateFromEmbedded("templatename.liquid", new ViewModel { Name = "Luke", Compliment = "Awesome" });
Because the Liquid templates can also be configured with an embedded provider, there are builder extensions that will configure both the embedded file provider for layouts and the EmbeddedTemplates
extensions.
There is a default of the executing assembly with Templates in EmailTemplates
builder.Services.AddFluentEmail("defaultfrom@email.com")
.AddLiquidRendererWithEmbedded(Assembly.GetCallingAssembly(), "AssemblyName.EmailTemplates")
// These are the same
builder.Services.AddFluentEmail("defaultfrom@email.com")
.AddLiquidRendererWithEmbedded()
If you want all templates in a folder to automatically be embedded, use the following in your csproj
file.
<ItemGroup>
<EmbeddedResource Include="EmailTemplates/**/*.liquid" />
</ItemGroup>
// 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" });
Note for .NET Core 2 users: You'll need to add the following line to the project containing any embedded razor views. See this issue for more details.
<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
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);