ABP VNext 利用QQ邮箱发送邮件
- 1.配置邮箱
- 1.1 设置
- 1.2账户
- 1.3 开启邮箱服务
- 1.4 生成授权码
- 2 默认的SmtpEmailSender发送邮件
- 2.1 生成数据库
- 2.2 添加一个后台任务执行发送邮件
- 2.3 服务中注入ISmtpEmailSender
- 2.4 在配置文件添加邮箱信息
- 3 集成MailKit发送邮件
- 3.1 添加包Volo.Abp.MailKit
- 3.2 Module文件配置依赖
- 3.3 服务中注入IEmailSender
- 3.4 在配置文件添加邮箱信息
1.配置邮箱
1.1 设置
1.2账户
1.3 开启邮箱服务
1.4 生成授权码
2 默认的SmtpEmailSender发送邮件
2.1 生成数据库
先修改数据库配置文件,执行迁移 ,不然项目启动不起来
2.2 添加一个后台任务执行发送邮件
Module文件的OnApplicationInitialization方法:
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
......
#region 后台服务
context.AddBackgroundWorkerAsync<MailWorker>();
#endregion
}
2.3 服务中注入ISmtpEmailSender
public class MailWorker : AsyncPeriodicBackgroundWorkerBase
{
private readonly ISmtpEmailSender _emailSender;
private readonly ISettingEncryptionService _settingEncryptionService;
private readonly ISettingDefinitionManager settingDefinitionManager;
public MailWorker(
AbpAsyncTimer timer,
IServiceScopeFactory serviceScopeFactory, ISmtpEmailSender emailSender, ISettingEncryptionService settingEncryptionService, ISettingDefinitionManager settingDefinitionManager
) : base(
timer,
serviceScopeFactory)
{
Timer.Period = 4000; //4s
_emailSender = emailSender;
_settingEncryptionService = settingEncryptionService;
this.settingDefinitionManager = settingDefinitionManager;
}
protected async override Task DoWorkAsync(
PeriodicBackgroundWorkerContext workerContext)
{
var setting = settingDefinitionManager.Get(EmailSettingNames.Smtp.Password);
var ss = _settingEncryptionService.Encrypt(setting, "zuqdlkgeyiyobbcd");
Logger.LogInformation("Starting: sending...");
await _emailSender.SendAsync(
"***@foxmail.com", "测试发送邮件", "how are you?"
);
Logger.LogInformation("Completed: ending...");
}
}
2.4 在配置文件添加邮箱信息
{
"App": {
"SelfUrl": "https://localhost:44370"
},
"ConnectionStrings": {
"Default": "Server=.;Database=SmtpEmailDemo;Uid=sa;Pwd=qwe123!;TrustServerCertificate=true"
},
"Settings": {
"Abp.Mailing.Smtp.Host": "smtp.qq.com",
"Abp.Mailing.Smtp.Port": "587",
"Abp.Mailing.Smtp.UserName": "***@qq.com",
"Abp.Mailing.Smtp.Password": "zuqdlkgeyiyobbcd",
"Abp.Mailing.Smtp.Domain": "",
"Abp.Mailing.Smtp.EnableSsl": "true",
"Abp.Mailing.Smtp.UseDefaultCredentials": "false",
"Abp.Mailing.DefaultFromAddress": "***@qq.com",
"Abp.Mailing.DefaultFromDisplayName": "***@qq.com"
},
"StringEncryption": {
"DefaultPassPhrase": "Yslb62n5falmPABy"
}
}
注意端口号是587 密码不用自己去加密,直接填写邮箱授权码就行,UseDefaultCredentials为false
3 集成MailKit发送邮件
重复2.1,2.2步骤
3.1 添加包Volo.Abp.MailKit
3.2 Module文件配置依赖
typeof(AbpMailKitModule)
3.3 服务中注入IEmailSender
public class MailWorker : AsyncPeriodicBackgroundWorkerBase
{
private readonly IEmailSender _emailSender;
public MailWorker(
AbpAsyncTimer timer,
IServiceScopeFactory serviceScopeFactory, IEmailSender emailSender
) : base(
timer,
serviceScopeFactory)
{
Timer.Period = 4000; //4s
_emailSender = emailSender;
}
protected async override Task DoWorkAsync(
PeriodicBackgroundWorkerContext workerContext)
{
Logger.LogInformation("Start: starting...");
await _emailSender.SendAsync(
"***@foxmail.com", "测试发送邮件", "how are you?"
);
Logger.LogInformation("Completed: ending...");
}
}
3.4 在配置文件添加邮箱信息
{
"App": {
"SelfUrl": "https://localhost:44373"
},
"ConnectionStrings": {
"Default": "Server=.;Database=MailTest;Uid=sa;Pwd=qwe123!;TrustServerCertificate=true"
},
"Settings": {
"Abp.Mailing.Smtp.Host": "smtp.qq.com",
"Abp.Mailing.Smtp.Port": "465",
"Abp.Mailing.Smtp.UserName": "***@qq.com",
"Abp.Mailing.Smtp.Password": "zuqdlkgeyiyobbcd",
"Abp.Mailing.Smtp.Domain": "",
"Abp.Mailing.Smtp.EnableSsl": "true",
"Abp.Mailing.Smtp.UseDefaultCredentials": "false",
"Abp.Mailing.DefaultFromAddress": "***@qq.com",
"Abp.Mailing.DefaultFromDisplayName": "***@qq.com"
},
"StringEncryption": {
"DefaultPassPhrase": "sSMpfXfpBMcXS9Iz"
}
}
注意端口号是465 密码不用自己去加密,直接填写邮箱授权码就行,UseDefaultCredentials为false