In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use springboot to integrate redis to send mailboxes and verify", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use springboot to integrate redis to send mailboxes and verify" this article.
1. Start
Pom file
Org.springframework.boot spring-boot-starter-redis 1.4.1.RELEASE org.springframework.boot spring-boot-starter-mail
Here is the yml configuration
# set port number server: port: 808 configure data source spring: mail: # QQ Mail this does not need to change host: smtp.qq.com # your email username: XX@qq.com # your authorization code password: XXXXXX default-encoding: UTF-8 redis: # redis server address host: XXXXXX # Redis server connection port port: 6379 # Redis server connection Password (default is empty) password: XXX jedis: pool: # maximum blocking wait time for connection pool (using negative values for no limit) max-wait: 1000 # maximum number of connections for connection pool (use negative values for no limit) max-active: 100 # maximum idle connections in connection pool max-idle: 20 # minimum idle connection in connection pool min-idle: 0 # connection timeout (milliseconds) timeout: 30000 email authorization code QQ Mail activate it if you don't know
two。 Tool mailbox tool package com.example.demo.util;/** * @ Classname MailServiceUtils * @ Description TODO * @ Author 86176 * @ Date 2021-12-17 15:04 * @ Version 2021 * * / import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.MailException;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.stereotype.Component @ Componentpublic class MailServiceUtils {private final Logger logger = LoggerFactory.getLogger (this.getClass ()); @ Autowired private JavaMailSender mailSender; / * @ param from sender * @ param to recipient * @ param subject topic * @ param content content * / public void sendMail (String from,String to, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage (); message.setFrom (from) Message.setTo (to); message.setSubject (subject); message.setText (content); try {mailSender.send (message); logger.info ("email sent successfully!");} catch (MailException e) {logger.error ("send email error:", e);}} redis garbled resolution package com.example.demo.config Import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer / * @ Classname Redisconfig * @ Description TODO * @ Author 86176 * @ Date 2021-12-06 10:02 * @ Version 86176 * / @ Configurationpublic class Redisconfig {@ Bean (name= "redisTemplate") public RedisTemplate redisTemplate (RedisConnectionFactory factory) {RedisTemplate template = new RedisTemplate (); RedisSerializer redisSerializer = new StringRedisSerializer (); template.setConnectionFactory (factory); / / key serialization mode template.setKeySerializer (redisSerializer) / / value serialize template.setValueSerializer (redisSerializer); / / value hashmap serialize template.setHashValueSerializer (redisSerializer); / / key haspmap serialize template.setHashKeySerializer (redisSerializer); / / return template;}} 3.controller build
Change as required
Package com.example.demo.controller;import com.example.demo.util.MailServiceUtils;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Controller;import org.springframework.util.Assert;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource / * * @ Classname EmailController * @ Description TODO email: * @ Author 86176 * @ Date 2021-12-17 15:28 * @ Version 2021 * * / @ Controllerpublic class EmailController {@ Resource private MailServiceUtils mailServiceUtils; @ Resource private RedisTemplate redisTemplate / * * send verification code redis storage verification code * @ param to mailbox account * @ return * / @ PostMapping ("/ fasong") @ ResponseBody public String email (String to) {try {/ / generate a 6-digit random number String I = String.valueOf ((int) ((Math.random () * 9 + 1) * 100000) / / send email mailServiceUtils.sendMail ("XXXXXX@qq.com", to, "CAPTCHA", I); / / redis save CAPTCHA redisTemplate.opsForValue () .set (to, I);} catch (Exception e) {return "error";} return "OK" } / * mailbox verification * @ param to mailbox account sent * @ param yzm input verification code * @ return * / @ PostMapping ("/ yz") @ ResponseBody public String yz (String to, String yzm) {/ / take out the verification code String o = (String) redisTemplate.opsForValue () .get (to) according to the mailbox account If (o.equals (yzm)) {return "OK";} return "No";} @ RequestMapping ("/ abc") public String abc () {return "QQemail";}} 4. Title receiver mailbox verification code / * send verification code * / $("# yzm") .click (function () {$.ajax ({url: "/ fasong", type: "post") Data: {"to": $("# to") .val ()} Success: function (data) {alert (data)}}) / * * CAPTCHA judgment * / $("# y") .click (function () {$.ajax ({url: "/ yz", type: "post") Data: {"to": $("# to") .val (), "yzm": $("# yz") .val ()}, success: function (data) {alert (data)})}) result
The above is all the contents of the article "how to use springboot to integrate redis to send mailboxes and verify". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.