SpringBoot 动态配置邮箱发件人过程解析
前言
现在的消息模块少不了邮件发送、短信发送和手机推送的功能。邮件发送的功能历史最为悠久,也算的上烂大街的功能。一般在配置文件中设置好邮箱地址、账号、密码和发件服务器地址后便不会再去改动。可是有的客户却希望人为指定发件人信息。这个需求并不过分,需要解决两个大问题:如何在容器启动成功后重新修改发送邮件的bean。如何在服务器重启后,发件人依然是更改后的配置信息。这里记录实现的步骤。
需求分析
一)、在未配置邮箱账号时,系统拥有默认的邮箱发件人
二)、重新设置邮箱发件人后,需立即生效
三)、重启服务器后,邮箱发件人依然是更改后的邮箱账号,而非默认发件人
基础的邮箱发送
邮箱发送的功能放在现在变得非常的简单好用,一导二配三发送。????
第一步:导入邮箱依赖包
compile('org.springframework.boot:spring-boot-starter-mail')
第二步:配置发件人邮箱信息
spring: mail: host: smtp.mxhichina.com username: itdragon@xx password: itdragon default-encoding: utf-8
第三步:发送邮件
@autowired lateinit var javamailsender: javamailsender fun pushmsgemail(target: string, subject: string, content: string) { if (target.isempty() || !pattern.matches(reg_email_format, target)) return val 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内存的角度看,这样做似乎没毛病!如果有不对的地方望不吝赐教????
@autowired lateinit var javamailsender: javamailsender fun configemail(postmailconfig: postmailconfig): javamailsender { val javamailsender = javamailsenderimpl() javamailsender.host = postmailconfig.mailhost javamailsender.username = postmailconfig.mailusername javamailsender.password = postmailconfig.mailpassword val javamailproperties = properties() javamailproperties["mail.smtp.auth"] = true javamailproperties["mail.smtp.starttls.enable"] = true javamailproperties["mail.smtp.timeout"] = 5000 javamailproperties["mail.smtp.socketfactory.class"] = "javax.net.ssl.sslsocketfactory" javamailproperties["mail.smtp.socketfactory.port"] = "465" javamailproperties["mail.smtp.port"] = "465" javamailsender.javamailproperties = javamailproperties this.javamailsender = javamailsender return 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) }
最后发送邮件的代码如下
@service class mailserviceimpl : mailservice { @value("\${spring.mail.username}") var mailusername: string? = null @autowired lateinit var javamailsender: javamailsender @autowired lateinit var systembaseconfigmapper: systembaseconfigmapper override fun pushmsgemail(target: string, subject: string, content: string) { if (target.isempty() || !pattern.matches(reg_email_format, target)) return val 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.mailhost javamailsender.username = postmailconfig.mailusername javamailsender.password = postmailconfig.mailpassword val javamailproperties = properties() javamailproperties["mail.smtp.auth"] = true javamailproperties["mail.smtp.starttls.enable"] = true javamailproperties["mail.smtp.timeout"] = 5000 javamailproperties["mail.smtp.socketfactory.class"] = "javax.net.ssl.sslsocketfactory" javamailproperties["mail.smtp.socketfactory.port"] = "465" javamailproperties["mail.smtp.port"] = "465" javamailsender.javamailproperties = javamailproperties this.javamailsender = javamailsender return javamailsender } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 浅谈springboot自动配置原理