ZKEACMS中的邮件通知以及消息扩展
ZKEASOFT March 29, 2018
发送邮件是系统里面一个非常常见的功能,ZKEACMS中通过Razor引擎使用cshtml视图模板来生成邮件内容,这样邮件内容的调整就变得非常方便
邮件配置
要发送邮件,首先要先设置好SMPT服务器,ZKEACMS的SMTP设置放在系统设置中/admin/SmtpSetting/Config,如果您是使用QQ邮箱作为SMTP服务器,可以先看看这个文章:http://www.zkea.net/codesnippet/detail/tencent-smtp.html
发送邮件
发送邮件的方式很简单,直接使用INotificationManager中的Send方法即可,例如找回密码的发送邮件代码:
public void ResetPassword(UserEntity user)
{
_notificationManager.Send(new RazorEmailNotice
{
Subject = "重置密码",
To = new string[] { user.Email },
Model = new ResetPasswordViewModel
{
Link = $"{_httpContextAccessor.HttpContext.Request.Scheme}://{_httpContextAccessor.HttpContext.Request.Host}/Account/Reset?token={user.ResetToken}&pt={_dataProtector.Protect(user.ResetToken)}"
},
TemplatePath = "~/EmailTemplates/ResetPassword.cshtml"
});
}
其中,Model,是模板ResetPassword.cshtml中使用的ViewModel,这个Model将会被传入到这个视图中,然后生成邮件的内容。
注意
如果是把邮件模板放在插件目录下,那TemplatePath的格式为:~/wwwroot/Plugins/{PluginName}/xx.cshtml
例如:
~/wwwroot/Plugins/ZKEACMS.Article/EmailTemplates/ResetPassword.cshtml
消息扩展
邮件其实也只是消息的一种,可以在开发过程中定义自己的消息(如:站内消息)内容和发送方式。自定义的方式也很简单,只需要实现Notice(消息内容)和INotifyService(消息发送方式)即可。实现自定义消息之后,只需要将INotifyService(消息发送方式)注册到容器中即可。
services.AddTransient<INotifyService, RazorEmailNotifyService>();
然后就可以通过INotificationManager来统一发送了。
_notificationManager.Send(new CustomerNotice{});
