您的位置:首页 > 其它

ABP入门系列(17)——使用ABP集成的邮件系统发送邮件

2017-01-07 21:54 513 查看
ABP中对邮件的封装主要集成在
Abp.Net.Mail
Abp.Net.Mail.Smtp
命名空间下,相应源码在此

#一、Abp集成的邮件模块是如何实现的



分析可以看出主要由以下几个核心类组成:

EmailSettingNames:静态常量类,主要定义了发送邮件需要的相关参数:Port、Host、UserName、Password、Domain、EnableSsl、UseDefaultCredentials。

EmailSettingProvider:继承自
SettingProvider
,对
EmailSettingNames
中定义的参数项进行设置。

SmtpEmailSenderConfiguration :继承自
EmailSenderConfiguration
,用来读取设置的支持Smtp协议邮件相关参数项。

SmtpEmailSender:继承自
EmailSenderBase
,实现了
ISmtpEmailSender
接口。该类就是基于SMTP协议进行邮件发送。提供了
SendEmailAsync(MailMessage mail)
SendEmail(MailMessage mail)
,同步异步两种发送邮件的方法。

想具体了解源码的实现方式,建议参考以下两篇博文:

结合ABP源码实现邮件发送功能

ABP源码分析七:Setting 以及 Mail

# 二、如何使用Abp集成的邮件系统发送邮件

1. 初始化邮件相关参数

在以
EntityFramework
结尾的项目中的
DefaultSettingsCreator
中添加默认设置,然后在程序包管理控制台执行
Update-DataBase
,这样即可把种子数据更新到数据库中。



代码中我是以QQ邮箱设置,有几点需要注意:

UserName即为QQ邮箱名,但Password并不是你QQ邮箱的登陆密码,而是授权码。授权码如何申请,请参考官方文档。否则发送邮件将会得到[Error: need EHLO and AUTH first !”] 异常。



Domain置空即可。

2. 代码调用示例

首先,在Service中通过构造函数注入
ISmtpEmailSenderConfiguration


private readonly IRepository<Task> _taskRepository;
private readonly IRepository<User, long> _userRepository;
private readonly ISmtpEmailSenderConfiguration _smtpEmialSenderConfig;

/// <summary>
///In constructor, we can get needed classes/interfaces.
///They are sent here by dependency injection system automatically.
/// </summary>
public TaskAppService(IRepository<Task> taskRepository, IRepository<User, long> userRepository,
ISmtpEmailSenderConfiguration smtpEmialSenderConfigtion)
{
_taskRepository = taskRepository;
_userRepository = userRepository;
_smtpEmialSenderConfig = smtpEmialSenderConfigtion;
}


在需要发送邮件的地方调用
SmtpEmailSender
类的发送方法即可。

SmtpEmailSender emailSender = new SmtpEmailSender(_smtpEmialSenderConfig);
string message = "You hava been assigned one task into your todo list.";
emailSender.Send("ysjshengjie@qq.com", task.AssignedPerson.EmailAddress, "New Todo item", message);




# 三、如何使用Abp集成的通知模块发送通知

直接上代码示例:

首先,在Service中通过构造函数注入
INotificationPublisher


/// <summary>
///In constructor, we can get needed classes/interfaces.
///They are sent here by dependency injection system automatically.
/// </summary>
public TaskAppService(IRepository<Task> taskRepository, IRepository<User, long> userRepository,
ISmtpEmailSenderConfiguration smtpEmialSenderConfigtion, INotificationPublisher notificationPublisher)
{
_taskRepository = taskRepository;
_userRepository = userRepository;
_smtpEmialSenderConfig = smtpEmialSenderConfigtion;
_notificationPublisher = notificationPublisher;
}


在需要发送通知的地方调用
INotificationPublisher
接口提供的
Publish
PublishAsync
方法即可;我们先来看看需要用到参数。



注意

NotificationData 是可选的,某些通知可能不需要数据。一些预定义的通知数据类型可能对于大多数情况够用了。 MessageNotificationData可以用于简单的信息, LocalizableMessageNotificationData可以用于本地化的,带参数的通知信息。

string message = "You hava been assigned one task into your todo list.";
_notificationPublisher.Publish("NewTask", new MessageNotificationData(message), null,NotificationSeverity.Info, new[] {task.AssignedPerson.ToUserIdentifier()});


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: