• SpringBoot项目整合Redis,Rabbitmq发送、消费、存储邮件


    📑前言

    本文主要是【Rabbitmq】——SpringBoot项目整合Redis,Rabbitmq发送、消费、存储邮件的文章,如果有什么需要改进的地方还请大佬指出⛺️

    🎬作者简介:大家好,我是听风与他🥇
    ☁️博客首页:CSDN主页听风与他
    🌄每日一句:狠狠沉淀,顶峰相见

    SpringBoot项目整合Redis,Rabbitmq发送、消费、存储邮件

    1.导入mail,redis,rabbitmq的依赖

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-amqpartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-mailartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2.配置application.yml文件

    spring:
      mail:
        host: smtp.163.com
        username: 15671190765@163.com
        password: XXX   #此为邮箱的snmp密码
      rabbitmq:
        addresses: localhost
        username: admin #rabbitmq的账号名密码均为admin
        password: admin 
        virtual-host: / #虚拟主机采用默认的/
      data:
        redis:
          port: 6379
          host: localhost #redis均为默认配置及端口,不配置yml也可
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3.Rabbitmq配置类:RabbitConfiguration

    package com.rabbitmqemail.config;
    
    import org.springframework.amqp.core.Queue;
    import org.springframework.amqp.core.QueueBuilder;
    import org.springframework.amqp.rabbit.connection.ConnectionFactory;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
    import org.springframework.amqp.support.converter.MessageConverter;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    
    @Configuration
    public class RabbitConfiguration {
    
        @Bean
        public MessageConverter messageConverter(){
            return new Jackson2JsonMessageConverter();
        }
    
        @Bean
        public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter converter) {
            RabbitTemplate template = new RabbitTemplate(connectionFactory);
            template.setMessageConverter(converter);
            return template;
        }
    
        //给Bean队列取名为邮件队列
        @Bean("emailQueue")
        public Queue emailQueue(){
            return QueueBuilder
                    .durable("mail")  //给邮件队列取名为email
                    .build();
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    Rabbitmq监听类:MailQueueListener

    package com.rabbitmqemail.listener;
    
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Component;
    
    import java.util.Map;
    @Component
    @RabbitListener(queues = "mail") //指定一下消息队列,该消息队列是mail消息队列
    public class MailQueueListener{
    
        @Autowired
        JavaMailSender sender;
    
        @Value("${spring.mail.username}")
        String username;
    
        @RabbitHandler
        public void sendMailMessage(Map<String,Object> data){
    //        System.out.println(data.get("email")+" "+data.get("code"));
            String email = (String) data.get("email");
            Integer code = (Integer) data.get("code");
            SimpleMailMessage  message= createMessage("欢迎注册我们的网站","您的验证码为"+code+",有效时间三分钟,为了保障您的安全,请勿向他人泄露验证码信息。",email);
            System.out.println("message1:"+message.getText());
            if (message == null) return;
            sender.send(message);
        }
    
        private SimpleMailMessage createMessage(String title,String content,String email){
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject(title);  //主题
            message.setText(content);   //内容
            message.setTo(email);       //发送目标邮箱
            message.setFrom(username);  //源发送邮箱
            return message;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    接口类:emailService

    package com.rabbitmqemail.service;
    
    public interface emailService {
        String EmailVerifyCode(String email);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    接口实现类:emailServiceImpl

    package com.rabbitmqemail.service.impl;
    
    
    import ch.qos.logback.classic.pattern.MessageConverter;
    import com.alibaba.fastjson2.JSONObject;
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmqemail.service.emailService;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    import java.util.Map;
    import java.util.Random;
    import java.util.concurrent.TimeUnit;
    
    @Service
    public class emailServiceImpl implements emailService {
    
        @Autowired
        AmqpTemplate amqpTemplate; //将消息队列注册为bean
    
        @Autowired
        StringRedisTemplate redisTemplate;
    
    
        @Override
        public String EmailVerifyCode(String email) {
            Random random = new Random();
            int code = random.nextInt(899999)+100000;  //生成六位数的验证码
    //        System.out.println("email:"+email+" code:"+code);
            Map<String,Object> data = Map.of("email",email,"code",code);
            amqpTemplate.convertAndSend("mail",data); //向消息队列中发送数据
            redisTemplate.opsForValue()
                    .set(email,String.valueOf(code),3, TimeUnit.MINUTES);
            //用redis来存取数据
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    测试类:RabbitmqEmailApplicationTests

    package com.rabbitmqemail;
    
    import com.rabbitmqemail.service.impl.emailServiceImpl;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class RabbitmqEmailApplicationTests {
    
    
        @Autowired
        private emailServiceImpl emailService;
    
        @Test
        void contextLoads() {
            emailService.EmailVerifyCode("2482893650@qq.com");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试结果:此时指定邮箱已收到验证码

    在这里插入图片描述

    测试项目开源仓库:

    https://gitee.com/zhang-zilong_zzl/Rabbitmq-email
    
    • 1

    📑文章末尾

    在这里插入图片描述

  • 相关阅读:
    Python获取Window系统注册表安装的软件,再卸载软件
    1.5 字符串基本操作(Python)
    C++内存管理
    ELK介绍以及搭建
    android8.1系统静默安装问题
    为什么一定要从DevOps走向BizDevOps?
    Java.lang.Class类 getFields()方法有什么功能呢?
    Linux:基础笔记
    中兴通讯5G交付能力出众,助力泰国True运营商实现海岛5G网络全覆盖
    Linux操作系统——类UNIX系统
  • 原文地址:https://blog.csdn.net/weixin_61494821/article/details/134631281