真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置郵箱發(fā)件人的過(guò)程解析

這篇文章主要講解了“SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置郵箱發(fā)件人的過(guò)程解析”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置郵箱發(fā)件人的過(guò)程解析”吧!

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了耀州免費(fèi)建站歡迎大家使用!

前言

現(xiàn)在的消息模塊少不了郵件發(fā)送、短信發(fā)送和手機(jī)推送的功能。郵件發(fā)送的功能歷史最為悠久,也算的上爛大街的功能。一般在配置文件中設(shè)置好郵箱地址、賬號(hào)、密碼和發(fā)件服務(wù)器地址后便不會(huì)再去改動(dòng)??墒怯械目蛻魠s希望人為指定發(fā)件人信息。這個(gè)需求并不過(guò)分,需要解決兩個(gè)大問(wèn)題:如何在容器啟動(dòng)成功后重新修改發(fā)送郵件的Bean。如何在服務(wù)器重啟后,發(fā)件人依然是更改后的配置信息。這里記錄實(shí)現(xiàn)的步驟。

需求分析

一)、在未配置郵箱賬號(hào)時(shí),系統(tǒng)擁有默認(rèn)的郵箱發(fā)件人

二)、重新設(shè)置郵箱發(fā)件人后,需立即生效

三)、重啟服務(wù)器后,郵箱發(fā)件人依然是更改后的郵箱賬號(hào),而非默認(rèn)發(fā)件人

基礎(chǔ)的郵箱發(fā)送郵箱發(fā)送的功能放在現(xiàn)在變得非常的簡(jiǎn)單好用,一導(dǎo)二配三發(fā)送。?

第一步:導(dǎo)入郵箱依賴包

compile('org.springframework.boot:spring-boot-starter-mail')第二步:配置發(fā)件人郵箱信息

spring:mail:host: smtp.mxhichina.comusername: itdragon@xxpassword: itdragondefault-encoding: utf-8第三步:發(fā)送郵件

@Autowiredlateinit var javaMailSender: JavaMailSender

fun pushMsgEmail(target: String, subject: String, content: String) {if (target.isEmpty() || !Pattern.matches(REG_EMAIL_FORMAT, target)) returnval mailMsg = SimpleMailMessage()mailMsg.setFrom(mailUserName!!)mailMsg.setTo(target)mailMsg.setSubject(subject)mailMsg.setText(content)javaMailSender.send(mailMsg)}可配置的郵件發(fā)送這里的可配置值的是配置郵箱的發(fā)件人。首先我們要解決第一個(gè)問(wèn)題,JavaMailSender 的Bean對(duì)象是在容器啟動(dòng)成功后就已經(jīng)注入到容器中。如何在容器啟動(dòng)后重新注入新的JavaMailSender 的Bean對(duì)象呢?網(wǎng)上找了一些案例,他們都是通過(guò)銷毀Bean然后再重新創(chuàng)建Bean的方式實(shí)現(xiàn)。我有點(diǎn)好奇地是,為什么不直接將新的對(duì)象直接賦值從而替換原有的Bean對(duì)象?Spring默認(rèn)是單例模式,從Java內(nèi)存的角度看,這樣做似乎沒(méi)毛??!如果有不對(duì)的地方望不吝賜教?

@Autowiredlateinit var javaMailSender: JavaMailSender

fun configEmail(postMailConfig: PostMailConfig): JavaMailSender {val javaMailSender = JavaMailSenderImpl()javaMailSender.host = postMailConfig.mailHostjavaMailSender.username = postMailConfig.mailUsernamejavaMailSender.password = postMailConfig.mailPasswordval javaMailProperties = Properties()javaMailProperties["mail.smtp.auth"] = truejavaMailProperties["mail.smtp.starttls.enable"] = truejavaMailProperties["mail.smtp.timeout"] = 5000javaMailProperties["mail.smtp.socketFactory.class"] = "javax.net.ssl.SSLSocketFactory"javaMailProperties["mail.smtp.socketFactory.port"] = "465"javaMailProperties["mail.smtp.port"] = "465"javaMailSender.javaMailProperties = javaMailPropertiesthis.javaMailSender = javaMailSenderreturn javaMailSender}再來(lái)解決第二個(gè)問(wèn)題,服務(wù)器重啟后,默認(rèn)情況下依然會(huì)重新加載application.yml中的配置信息。這會(huì)出現(xiàn)郵箱發(fā)件人和實(shí)際配置的發(fā)件人不匹配的情況。其實(shí)這個(gè)問(wèn)題也很好解決,加一個(gè)事件監(jiān)聽(tīng)器,在容器初始化成功后執(zhí)行,根據(jù)之前保存的郵箱信息,重新配置郵箱。當(dāng)然,我們需要一張表記錄當(dāng)前發(fā)件人信息。

// 創(chuàng)建事件監(jiān)聽(tīng)器class ApplicationStartup : ApplicationListener {override fun onApplicationEvent(contextRefreshedEvent: ContextRefreshedEvent) {val systemBaseConfigMapper = contextRefreshedEvent.applicationContext.getBean(SystemBaseConfigMapper::class.java)val postMailConfig = systemBaseConfigMapper.selectByMail()val mailService = contextRefreshedEvent.applicationContext.getBean(MailService::class.java)mailService.configEmail(postMailConfig)}}

// 注冊(cè)事件監(jiān)聽(tīng)器fun main(args: Array) {val springApplication = SpringApplication(StartApplication::class.java)springApplication.addListeners(ApplicationStartup())springApplication.run(*args)}最后發(fā)送郵件的代碼如下

@Serviceclass MailServiceImpl : MailService {

@Value("\${spring.mail.username}")var mailUserName: String? = null

@Autowiredlateinit var javaMailSender: JavaMailSender@Autowiredlateinit var systemBaseConfigMapper: SystemBaseConfigMapper

override fun pushMsgEmail(target: String, subject: String, content: String) {if (target.isEmpty() || !Pattern.matches(REG_EMAIL_FORMAT, target)) returnval mailMsg = SimpleMailMessage()mailMsg.setFrom(mailUserName!!)mailMsg.setTo(target)mailMsg.setSubject(subject)mailMsg.setText(content)try {systemBaseConfigMapper.selectByMailName()?.let {mailMsg.setFrom(it.value!!)}javaMailSender.send(mailMsg)} catch (e: Exception) {e.printStackTrace()}}

override fun configEmail(postMailConfig: PostMailConfig): JavaMailSender {val javaMailSender = JavaMailSenderImpl()javaMailSender.host = postMailConfig.mailHostjavaMailSender.username = postMailConfig.mailUsernamejavaMailSender.password = postMailConfig.mailPasswordval javaMailProperties = Properties()javaMailProperties["mail.smtp.auth"] = truejavaMailProperties["mail.smtp.starttls.enable"] = truejavaMailProperties["mail.smtp.timeout"] = 5000javaMailProperties["mail.smtp.socketFactory.class"] = "javax.net.ssl.SSLSocketFactory"javaMailProperties["mail.smtp.socketFactory.port"] = "465"javaMailProperties["mail.smtp.port"] = "465"javaMailSender.javaMailProperties = javaMailPropertiesthis.javaMailSender = javaMailSenderreturn javaMailSender}

}

感謝各位的閱讀,以上就是“SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置郵箱發(fā)件人的過(guò)程解析”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置郵箱發(fā)件人的過(guò)程解析這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!


當(dāng)前標(biāo)題:SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置郵箱發(fā)件人的過(guò)程解析
網(wǎng)站地址:http://weahome.cn/article/jjiiii.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部