您当前的位置: 首页 >  .net

寒冰屋

暂无认证

  • 1浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

寒冰屋 发布时间:2022-05-03 11:02:59 ,浏览量:1

目录

简介

Nuget包

用法

基本用法

依赖注入

使用 Razor 模板

使用 Liquid 模板

发送电子邮件

Github地址

 

简介

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

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0484s