FluentEmail - 适用于 .NET 和 .NET Core 的邮件发送库

简介

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生态圈越来越好!

8249dfbdca73c5811be756975ec97439.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值