📑前言
本文主要是【Rabbitmq】——SpringBoot项目整合Redis,Rabbitmq发送、消费、存储邮件的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
SpringBoot项目整合Redis,Rabbitmq发送、消费、存储邮件
1.导入mail,redis,rabbitmq的依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
2.配置application.yml文件
spring:mail:host: smtp.163.com
username: [email protected]
password: XXX #此为邮箱的snmp密码rabbitmq:addresses: localhost
username: admin #rabbitmq的账号名密码均为adminpassword: admin
virtual-host: / #虚拟主机采用默认的/data:redis:port:6379host: localhost #redis均为默认配置及端口,不配置yml也可
3.Rabbitmq配置类:RabbitConfiguration
packagecom.rabbitmqemail.config;importorg.springframework.amqp.core.Queue;importorg.springframework.amqp.core.QueueBuilder;importorg.springframework.amqp.rabbit.connection.ConnectionFactory;importorg.springframework.amqp.rabbit.core.RabbitTemplate;importorg.springframework.amqp.support.converter.Jackson2JsonMessageConverter;importorg.springframework.amqp.support.converter.MessageConverter;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassRabbitConfiguration{@BeanpublicMessageConvertermessageConverter(){returnnewJackson2JsonMessageConverter();}@BeanpublicRabbitTemplaterabbitTemplate(ConnectionFactory connectionFactory,MessageConverter converter){RabbitTemplate template =newRabbitTemplate(connectionFactory);
template.setMessageConverter(converter);return template;}//给Bean队列取名为邮件队列@Bean("emailQueue")publicQueueemailQueue(){returnQueueBuilder.durable("mail")//给邮件队列取名为email.build();}}
Rabbitmq监听类:MailQueueListener
packagecom.rabbitmqemail.listener;importorg.springframework.amqp.rabbit.annotation.RabbitHandler;importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.mail.SimpleMailMessage;importorg.springframework.mail.javamail.JavaMailSender;importorg.springframework.stereotype.Component;importjava.util.Map;@Component@RabbitListener(queues ="mail")//指定一下消息队列,该消息队列是mail消息队列publicclassMailQueueListener{@AutowiredJavaMailSender sender;@Value("${spring.mail.username}")String username;@RabbitHandlerpublicvoidsendMailMessage(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);}privateSimpleMailMessagecreateMessage(String title,String content,String email){SimpleMailMessage message =newSimpleMailMessage();
message.setSubject(title);//主题
message.setText(content);//内容
message.setTo(email);//发送目标邮箱
message.setFrom(username);//源发送邮箱return message;}}
接口类:emailService
packagecom.rabbitmqemail.service;publicinterface emailService {StringEmailVerifyCode(String email);}
接口实现类:emailServiceImpl
packagecom.rabbitmqemail.service.impl;importch.qos.logback.classic.pattern.MessageConverter;importcom.alibaba.fastjson2.JSONObject;importcom.rabbitmq.client.ConnectionFactory;importcom.rabbitmqemail.service.emailService;importorg.springframework.amqp.core.AmqpTemplate;importorg.springframework.amqp.rabbit.core.RabbitTemplate;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Bean;importorg.springframework.data.redis.core.StringRedisTemplate;importorg.springframework.stereotype.Service;importjava.util.Map;importjava.util.Random;importjava.util.concurrent.TimeUnit;@Servicepublicclass emailServiceImpl implements emailService {@AutowiredAmqpTemplate amqpTemplate;//将消息队列注册为bean@AutowiredStringRedisTemplate redisTemplate;@OverridepublicStringEmailVerifyCode(String email){Random random =newRandom();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来存取数据returnnull;}}
测试类:RabbitmqEmailApplicationTests
packagecom.rabbitmqemail;importcom.rabbitmqemail.service.impl.emailServiceImpl;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;@SpringBootTestclassRabbitmqEmailApplicationTests{@Autowiredprivate emailServiceImpl emailService;@TestvoidcontextLoads(){emailService.EmailVerifyCode("[email protected]");}}
测试结果:此时指定邮箱已收到验证码
测试项目开源仓库:
https://gitee.com/zhang-zilong_zzl/Rabbitmq-email
📑文章末尾
本文转载自: https://blog.csdn.net/weixin_61494821/article/details/134631281
版权归原作者 听风与他 所有, 如有侵权,请联系我们删除。
版权归原作者 听风与他 所有, 如有侵权,请联系我们删除。