maven依赖
<!--rabbitmq--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!--mail依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
1. 测试方法 controller 生产者
@GetMapping("/test1") public void test(HttpServletRequest request) throws Exception { String authorization = request.getHeader("Authorization"); String username = (String) JwtUtils.parseJWT(authorization).get("username"); HashMap<String, String> map = new HashMap<>(); map.put("username",username); map.put("msg","开始发送邮件"); try { rabbitTemplate.convertAndSend("name_exchange","name_routingKey",map);//指定交换机 路由key } catch (AmqpException e) { log.info("生产者失败"); } log.info("test执行了"); }
2 .消费者--监听
@Component public class Listener { @Autowired private JavaMailSender javaMailSender; @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "scan_queue", durable = "false"), //队列名 是否持久化 exchange = @Exchange( value = "name_exchange", //交换机配置 ignoreDeclarationExceptions = "true", type = ExchangeTypes.TOPIC ), key = {"scan_routingKey"})) public void listen(Map map) { System.out.println("接收到消息:" + map.get("msg")); SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setSubject("尊敬的用"+map.get("username")+"户您好!"); simpleMailMessage.setText("扫描完毕"); simpleMailMessage.setTo("[email protected]"); simpleMailMessage.setFrom("[email protected]"); javaMailSender.send(simpleMailMessage); } }
邮件和rabbitmq配置
mail: host: smtp.163.com protocol: smtp default-encoding: utf-8 password: ****** username: *****@163.com port: 25 rabbitmq: username: dev password: **** host: localhost port: 5672 virtual-host: /dev template: retry: enabled: true initial-interval: 2000ms max-interval: 10000ms multiplier: 2 publisher-confirm-type: correlated
标签:username,map,springboot,get,--,boot,simpleMailMessage,com,yml From: https://www.cnblogs.com/caoaman/p/17367958.html