SpringBoot 动态配置邮箱发件人
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
}
}
文章到这里就结束了,感谢阅读!ths!
出处:http://www.cnblogs.com/itdragon/
简介:每天都在进步,每周都在总结,架构师的修罗之路!你的一个点赞,一句留言,就可以让博主开心一笑,充满动力!ITDragon的微信公众号学英语会编程只有英语学的好,源码才能看的爽
版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。