这篇文章主要讲解了“SpringBoot实现动态配置邮箱发件人的过程解析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot实现动态配置邮箱发件人的过程解析”吧!
前言
现在的消息模块少不了邮件发送、短信发送和手机推送的功能。邮件发送的功能历史最为悠久,也算的上烂大街的功能。一般在配置文件中设置好邮箱地址、账号、密码和发件服务器地址后便不会再去改动。可是有的客户却希望人为指定发件人信息。这个需求并不过分,需要解决两个大问题:如何在容器启动成功后重新修改发送邮件的Bean。如何在服务器重启后,发件人依然是更改后的配置信息。这里记录实现的步骤。
需求分析
一)、在未配置邮箱账号时,系统拥有默认的邮箱发件人
二)、重新设置邮箱发件人后,需立即生效
三)、重启服务器后,邮箱发件人依然是更改后的邮箱账号,而非默认发件人
基础的邮箱发送邮箱发送的功能放在现在变得非常的简单好用,一导二配三发送。?
第一步:导入邮箱依赖包
compile('org.springframework.boot:spring-boot-starter-mail')第二步:配置发件人邮箱信息
spring:mail:host: smtp.mxhichina.comusername: itdragon@xxpassword: itdragondefault-encoding: utf-8第三步:发送邮件
@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)}可配置的邮件发送这里的可配置值的是配置邮箱的发件人。首先我们要解决第一个问题,JavaMailSender 的Bean对象是在容器启动成功后就已经注入到容器中。如何在容器启动后重新注入新的JavaMailSender 的Bean对象呢?网上找了一些案例,他们都是通过销毁Bean然后再重新创建Bean的方式实现。我有点好奇地是,为什么不直接将新的对象直接赋值从而替换原有的Bean对象?Spring默认是单例模式,从Java内存的角度看,这样做似乎没毛病!如果有不对的地方望不吝赐教?
@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}再来解决第二个问题,服务器重启后,默认情况下依然会重新加载application.yml中的配置信息。这会出现邮箱发件人和实际配置的发件人不匹配的情况。其实这个问题也很好解决,加一个事件监听器,在容器初始化成功后执行,根据之前保存的邮箱信息,重新配置邮箱。当然,我们需要一张表记录当前发件人信息。
// 创建事件监听器class ApplicationStartup : ApplicationListener<ContextRefreshedEvent> {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)}}
// 注册事件监听器fun main(args: Array<String>) {val springApplication = SpringApplication(StartApplication::class.java)springApplication.addListeners(ApplicationStartup())springApplication.run(*args)}最后发送邮件的代码如下
@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实现动态配置邮箱发件人的过程解析”的内容了,经过本文的学习后,相信大家对SpringBoot实现动态配置邮箱发件人的过程解析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。