c发送html邮件,ASP.NET Core发送HTML格式邮件(Email)方法及示例代码

1、安装引用FluentEmail方法

2、Startup.cs中ConfigureServices(IServiceCollection services)中配置// 配置 IFluentEmail

services.AddFluentEmail(Configuration["email_address"])

.AddRazorRenderer()

.AddSmtpSender(new SmtpClient("smtp.gmail.com", 587)

{

Credentials = new NetworkCredential(Configuration["email_address"], Configuration["email_password"]),

EnableSsl = true

});

// 添加 service

services.TryAddScoped();

配置示例中使用GMAIL帐户发送电子邮件,可以使用所需的任何电子邮件服务器。

3、使用FluentEmail发送邮件示例代码//通过模板发送邮件的接口

public interface IEmailSender

{

Task SendUsingTemplate(string to, string subject, EmailTemplate template, object model);

}

//邮件模板

public enum EmailTemplate

{

EmailConfirmation,

ChangeEmail

}

public class EmailSender : IEmailSender

{

//需要更改的是TemplatePath常量,该常量需要包含模板文件(.cshtml文件)所在的完整命名空间名称。

private const string TemplatePath = "Web.Api.Infrastructure.Services.Emails.Templates.{0}.cshtml";

private readonly IFluentEmail _email;

private readonly ILogger _logger;

public EmailSender(IFluentEmail email, ILogger logger)

{

_email = email;

_logger = logger;

}

public async Task SendUsingTemplate(string to, string subject, EmailTemplate template, object model)

{

var result = await _email.To(to)

.Subject(subject)

.UsingTemplateFromEmbedded(string.Format(TemplatePath, template), ToExpando(model), GetType().Assembly)

.SendAsync();

if (!result.Successful)

{

_logger.LogError("Failed to send an email.\n{Errors}", string.Join(Environment.NewLine, result.ErrorMessages));

}

return result.Successful;

}

}

注意:在模板中使用匿名对象,我们需要将其转换为Expando objects。这是RazorLight(FluentEmail用于处理Razor模板的库)所具有的“限制” 。private static ExpandoObject ToExpando(object model)

{

if (model is ExpandoObject exp)

{

return exp;

}

IDictionary expando = new ExpandoObject();

foreach (var propertyDescriptor in model.GetType().GetTypeInfo().GetProperties())

{

var obj = propertyDescriptor.GetValue(model);

if (obj != null && IsAnonymousType(obj.GetType()))

{

obj = ToExpando(obj);

}

expando.Add(propertyDescriptor.Name, obj);

}

return (ExpandoObject)expando;

}

private static bool IsAnonymousType(Type type)

{

bool hasCompilerGeneratedAttribute = type.GetTypeInfo()

.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false)

.Any();

bool nameContainsAnonymousType = type.FullName.Contains("AnonymousType");

bool isAnonymousType = hasCompilerGeneratedAttribute && nameContainsAnonymousType;

return isAnonymousType;

}

3、嵌入cshtml模板

上面代码引用cshtml模板需要嵌入资源,要cshtml模板打包在程序集中,在cshtml模板文件中属性窗口:"生成操作" =》选择 "嵌入的资源"

相关文档:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值