简介
FluentEmail - 适用于 .NET 和 .NET Core 的邮件发送库
从 .NET 和 .NET Core 发送电子邮件的最简单方法。将 Razor 用于电子邮件模板并使用 SendGrid、MailGun、SMTP 等进行发送
Nuget包
•FluentEmail.Core - 域模型•FluentEmail.Smtp - 通过 SMTP 服务器发送电子邮件•FluentEmail.Razor - 使用 Razor 模板生成电子邮件•FluentEmail.Liquid - 使用Liquid 模板生成电子邮件•FluentEmail.Mailgun - 通过 MailGun 的 REST API 发送电子邮件•FluentEmail.SendGrid - 通过 SendGrid API 发送电子邮件•FluentEmail.Mailtrap - 向 Mailtrap 发送电子邮件。使用FluentEmail.Smtp进行传递•FluentEmail.MailKit - 使用MailKit电子邮件库发送电子邮件
用法
基本用法
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();
依赖注入
public void ConfigureServices(IServiceCollection services)
{services.AddFluentEmail("fromemail@test.test").AddRazorRenderer().AddSmtpSender("localhost", 25);
}
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();}
}
使用 Razor 模板
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 模板
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" });
发送电子邮件
Email.DefaultSender = new SmtpSender();//send normally
email.Send();//send asynchronously
await email.SendAsync();
Github地址
https://github.com/lukencode/FluentEmail
最后大家如果喜欢我的文章,还麻烦给个关注并点个赞, 希望net生态圈越来越好!