ABP入門系列(17)——使用ABP集成的郵件系統發送郵件


ABP中對郵件的封裝主要集成在Abp.Net.MailAbp.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接口提供的PublishPublishAsync方法即可;我們先來看看需要用到參數。

注意

  • 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()});

成功接收通知


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM