Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringBoot realizes the process parsing of dynamically configuring mailbox sender

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "SpringBoot to achieve dynamic configuration of mailbox sender process resolution", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "SpringBoot to achieve dynamic configuration mailbox sender process parsing" bar!

Preface

The current message module can not do without the functions of e-mail sending, SMS sending and mobile phone pushing. The function of sending mail has the longest history, and it can be regarded as a function on a bad street. Generally, the email address, account number, password and the address of the sending server will not be changed after setting up the email address in the configuration file. However, some customers want to specify the sender information artificially. This requirement is not excessive, and two big problems need to be solved: how to re-modify the Bean that sends the mail after the container starts successfully. After the server restart, the sender is still the changed configuration information. The steps of the implementation are recorded here.

Demand analysis

1) when the mailbox account is not configured, the system has the default mailbox sender

2) after the mailbox sender is reset, it needs to take effect immediately

3) after restarting the server, the mailbox sender is still the changed mailbox account, not the default sender

The basic function of mailbox sending mailbox is now very simple and easy to use, one guide two with three send. ?

Step 1: import mailbox dependency packages

Compile ('org.springframework.boot:spring-boot-starter-mail') step 2: configure sender mailbox information

Spring:mail:host: smtp.mxhichina.comusername: itdragon@xxpassword: itdragondefault-encoding: utf-8 step 3: send email

@ 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 (mailUserNameplate!) mailMsg.setTo (target) mailMsg.setSubject (subject) mailMsg.setText (content) javaMailSender.send (mailMsg)} Configurable value here is the sender who configures the mailbox. First of all, we need to solve the first problem. The Bean object of JavaMailSender has been injected into the container after the container has been started successfully. How to re-inject the Bean object of the new JavaMailSender after the container starts? Some cases were found on the Internet, all of which were achieved by destroying the Bean and then recreating the Bean. I'm a little curious, why not just assign the new object directly to replace the original Bean object? Spring defaults to singleton mode, which seems fine from a Java memory point of view! If there is anything wrong, please do not hesitate to give me advice?

@ 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} to solve the second problem After the server is restarted, the configuration information in application.yml is still reloaded by default. This will cause a mismatch between the mailbox sender and the actual configured sender. In fact, this problem is also very easy to solve. Add an event listener, execute after the container is initialized successfully, and reconfigure the mailbox according to the previously saved mailbox information. Of course, we need a table to record the current sender information.

/ / create event listener 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)}}

/ / Registration event listener fun main (args: Array) {val springApplication = SpringApplication (StartApplication::class.java) springApplication.addListeners (ApplicationStartup ()) springApplication.run (* args)} the last code to send the email is as follows

@ 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 (mailUserNameplate!) mailMsg.setTo (target) mailMsg.setSubject (subject) mailMsg.setText (content) try {systemBaseConfigMapper.selectByMailName ()? .let {mailMsg.setFrom (it. Valueholders!)} 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 = 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}

}

Thank you for your reading, the above is the content of "SpringBoot to achieve dynamic configuration of mailbox sender process parsing". After the study of this article, I believe you have a deeper understanding of SpringBoot to achieve dynamic configuration mailbox sender process parsing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report