ABP框架系列之二十四:(Email-Sending-EF-电子邮件发送)
Introduction
Email sending is a pretty common task for almost every application. ASP.NET Boilerplate provides a basic infrastructure to simply send emails and seperate email server configuration from sending emails.
IEmailSender
IEmailSender is a service to simply send emails without knowing details. Example usage:
public class TaskManager : IDomainService
{
private readonly IEmailSender _emailSender; public TaskManager(IEmailSender emailSender)
{
_emailSender = emailSender;
} public void Assign(Task task, Person person)
{
//Assign task to the person
task.AssignedTo = person; //Send a notification email
_emailSender.Send(
to: person.EmailAddress,
subject: "You have a new task!",
body: $"A new task is assigned for you: <b>{task.Title}</b>",
isBodyHtml: true
);
}
}
We simply injected IEmailSender and used Send method. Send method has a few more overloads. It can also get a MailMessage object (not available for .net core since .net core does not include SmtpClient and MailMessage).
我们简单用注射IEmailSender和发送方法。发送方法有更多重载。它也可以让一个MailMessage对象(不可用。NET的核心从.NET核心不包括SmtpClient和邮件)。
ISmtpEmailSender
There is also ISmtpEmailSender which extends IEmailSender and adds BuildClient method to create an SmtpClient to directly use it (not available for .net core since .net core does not include SmtpClient and MailMessage). Using IEmailSender will be enough for most cases.
也有ismtpemailsender继承 iemailsender和添加方法用于创建在一buildclient SmtpClient直接使用它(不可用,因为.NET .NET核心的核心不包含MailMessage和SmtpClient)。使用iemailsender要足够的大多数例子。
NullEmailSender
There is also a null object pattern implementation of IEmailSender as NullEmailSender. You can use it in unit tests or injecting IEmalSender with property injection pattern.
Configuration
Email Sender uses setting management system to read emal sending configuration. All setting names are defined in Abp.Net.Mail.EmailSettingNames class as constant strings. Their values and descriptions:
电子邮件发件人使用设置管理系统读取邮件发送配置。所有设定的名字是在类的定义abp.net.mail.emailsettingnames常量字符串。他们的值和描述:
- Abp.Net.Mail.DefaultFromAddress: Used as sender email address when you don't specify a sender while sending emails (as like in the sample above).
- Abp.Net.Mail.DefaultFromDisplayName: Used as sender display name when you don't specify a sender while sending emails (as like in the sample above).
- Abp.Net.Mail.Smtp.Host: IP/Domain of the SMTP server (default: 127.0.0.1).
- Abp.Net.Mail.Smtp.Port: Port of the SMTP server (default: 25).
- Abp.Net.Mail.Smtp.UserName: Username, if SMTP server requires authentication.
- Abp.Net.Mail.Smtp.Password: Password, if SMTP server requires authentication.
- Abp.Net.Mail.Smtp.Domain: Domain for the username, if SMTP server requires authentication.
- Abp.Net.Mail.Smtp.EnableSsl: A value indicates that SMTP server uses SSL or not ("true" or "false". Default: "false").
- Abp.Net.Mail.Smtp.UseDefaultCredentials: True, to use default credentials instead of provided username and password ("true" or "false". Default: "true").
MailKit Integration
Since .net core does not support standard System.Net.Mail.SmtpClient, so we need a 3rd-party vendor to send emails. Fortunately, MailKit provides a good replacement for default SmtpClient. It's also suggested by Microsoft.
Abp.MailKit package gracefully integrates to ABP's email sending system. So, you can still use IEmailSender as described above to send emails via MailKit.
Installation
First, install Abp.MailKit nuget package to your project:
Install-Package Abp.MailKit
Integration
Add AbpMailKitModule to dependencies of your module:
[DependsOn(typeof(AbpMailKitModule))]
public class MyProjectModule : AbpModule
{
//...
}
Usage
You can use IEmailSender as described before since Abp.MailKit package registers MailKit implementation for it. It also uses the same configuration defined above.
Customization
You may need to make additional configuration or customization while creating MailKit's SmtpClient. In that case, you can replace IMailKitSmtpBuilder interface with your own implementation. You can derive from DefaultMailKitSmtpBuilder to make it easier. For instance, you may want to accept all SSL certificates. In that case, you can override ConfigureClient method as shown below:
你可能需要进行额外的配置或定制的同时创造MailKit SmtpClient。在这种情况下,你可以用你自己的imailkitsmtpbuilder接口实现替换。你可以从defaultmailkitsmtpbuilder更容易。例如,您可能希望接受所有SSL证书。在这种情况下,您可以重写configureclient方法如下图所示:
public class MyMailKitSmtpBuilder : DefaultMailKitSmtpBuilder
{
public MyMailKitSmtpBuilder(ISmtpEmailSenderConfiguration smtpEmailSenderConfiguration)
: base(smtpEmailSenderConfiguration)
{
} protected override void ConfigureClient(SmtpClient client)
{
client.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; base.ConfigureClient(client);
}
}
Then you can replace IMailKitSmtpBuilder interface with your implementation in PreInitialize method of your module:
[DependsOn(typeof(AbpMailKitModule))]
public class MyProjectModule : AbpModule
{
public override void PreInitialize()
{
Configuration.ReplaceService<IMailKitSmtpBuilder, MyMailKitSmtpBuilder>();
} //...
}
(remember to add "using Abp.Configuration.Startup;" statement since ReplaceService extension method is defined in that namespace)
(记得添加“使用ABP。配置。启动;”声明后,replaceservice扩展方法是定义在命名空间)
ABP框架系列之二十四:(Email-Sending-EF-电子邮件发送)的更多相关文章
- ABP框架系列之二十:(Dependency-Injection-依赖注入)
What is Dependency Injection If you already know Dependency Injection concept, Constructor and Prope ...
- ABP框架系列之二十六:(EventBus-Domain-Events-领域事件)
In C#, a class can define own events and other classes can register it to be notified when something ...
- ABP框架系列之二十二:(Dynamic-Web-API-动态WebApi)
Building Dynamic Web API Controllers This document is for ASP.NET Web API. If you're interested in A ...
- ABP框架系列之二十八:(Handling-Exceptions-异常处理)
Introduction This document is for ASP.NET MVC and Web API. If you're interested in ASP.NET Core, see ...
- ABP框架系列之二十五:(Embedded-Resource-Files-嵌入式资源文件)
Introduction ASP.NET Boilerplate provides an easy way of using embedded Razor views (.cshtml files) ...
- ABP框架系列之二十九:(Hangfire-Integration-延迟集成)
Introduction Hangfire is a compherensive background job manager. You can integrate ASP.NET Boilerpla ...
- 《sed的流艺术之四》-linux命令五分钟系列之二十四
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- ABP源码分析二十四:Notification
NotificationDefinition: 用于封装Notification Definnition 的信息.注意和Notification 的区别,如果把Notification看成是具体的消息 ...
- ABP框架系列之二:(Entity Framework Core-实体核心框架)
Introduction(介绍) Abp.EntityFrameworkCore nuget package is used to integrate to Entity Framework (EF) ...
随机推荐
- JAVA使用log4j(另SSM框架中使用log4j)
1.引入jar包 log4j-1.2.13.jar 2.src下建立配置文件:log4j.properties #不+All,只写后一种LOG log4j.rootLogger =ALL,system ...
- 构建一个Vue项目
一 我们需要安装vue.js Vue.js官网 当我们已经安装了vue-cli,那么我们需要更新Vue-cli. vue-cli3.0使用及配置 二 安装好了之后: 我们可以直接使用命令:mkdir ...
- Problem C: 重复子串(string)
/* 一个性质? right集合中只有相邻的位置才会有用 那么考虑set启发式合并, 能够求出大概nlogn个有用的对 那么将这些对按照右端点排序, 查询也按照右端点排序就可以离线维护信息 然后需要维 ...
- SpringBoot入门篇--关于properties和yml两种配置文件的一些事情
我们在使用SpringBoot这个框架的时候都一定使用或者说是见到过application.properties或者是application.yml,经不住有人就会问这俩文件到底是什么情况,其实说白了 ...
- JAVA性能优化:35个小细节让你提升java代码的运行效率
代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...
- spring boot 错误处理之深度历险
今天终于把 boot 的异常处理完全研究透了: boot提供了很多错误的处理工作.默认情况下,我们会看到一个whiteLabel(白标)的页面. 这个可能不是我们所需.因此我们需要定制.我于是做了个深 ...
- 阿里云视频直播PHP-SDK
阿里云 视频直播 配置 及 PHP-SDK 接入教程准备工作域名管理配置鉴权地址生成器及DEMO演示-熟悉鉴权接入SDK推流回调的配置阿里云 视频直播 配置 及 PHP-SDK 接入教程 个人感觉,阿 ...
- 深度学习原理与框架-CNN在文本分类的应用 1.tf.nn.embedding_lookup(根据索引数据从数据中取出数据) 2.saver.restore(加载sess参数)
1. tf.nn.embedding_lookup(W, X) W的维度为[len(vocabulary_list), 128], X的维度为[?, 8],组合后的维度为[?, 8, 128] 代码说 ...
- 数据预处理:独热编码(One-Hot Encoding)和 LabelEncoder标签编码
一.问题由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 离散特征的编码分为两种情况: 1.离散特征的取值之间没有大小的意义,比如color:[red,blue],那么就使用one- ...
- 使用wireshark以及filddler配合抓去手机端的TCP以及HTTP请求
在测试手机客户端时,有时候需要查看网络请求状况.使用在IDE中查看log的方式,能够解决问题,但是会比较复杂.wireshark不能够做代理,而fiddler主要是抓HTTP请求,没有wireshar ...