flask-mail异步发送邮件_springboot整合ActiveMQ消息组件实现邮件发送

e4980ced761800444564de5c99c5a2e5.png
1.在父项目springboot2.0-ActiveMQ中添加依赖 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-activemq com.alibaba fastjson 1.2.62org.springframework.boot spring-boot-starter-mailorg.apache.commons commons-lang32.springboot2.0-producer生产者2.1application.yml配置spring: activemq:  ##mq连接通信地址 broker-url: tcp://127.0.0.1:61616 user: admin password: admin##自定义队列my_queue: springboot2.0-queueserver: port: 8081##my_topic: springboot2.0-topic##server:## port: 80842.2注入Queuepackage com.cn.activemq;import javax.jms.Queue;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;@Componentpublic class ConfigQueue { @Value("${my_queue}")  private String myQueue;  @Bean //1.将队列注入到springboot容器中  public Queue queue() {  return new ActiveMQQueue(myQueue);  }}2.3Producer生产者代码package com.cn.activemq;import javax.jms.Queue;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import com.alibaba.fastjson.JSONObject;@Componentpublic class Producer {//每隔五秒向消息队列中发送消息@Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;@Autowiredprivate Queue queue;@Scheduled(fixedDelay = 5000)public void sendEmail() {String userName = System.currentTimeMillis()+"";JSONObject jsonObject = new JSONObject();jsonObject.put("userName", userName);jsonObject.put("email", "yangdaoyx@163.com");String msg = jsonObject.toString();jmsMessagingTemplate.convertAndSend(queue,msg);System.out.println("采用点对点通讯模式:"+msg);}}3.springboot2.0-consumer消费者3.1application.ymlspring: activemq:  ##mq连接通信地址 broker-url: tcp://127.0.0.1:61616 user: admin password: admin mail:  host: smtp.163.com username: yangdaoyx@163.com password: zzx1015 enable: true smtp:  auth: true starttls:  enable: true required: true ##开启发布订阅 ##jms:  ## pub-sub-domain: true##自定义队列my_queue: springboot2.0-queueserver: port: 8080##my_topic: springboot2.0-topic##server:## port: 80863.2Consumer 消费者异步代码package com.cn.activemq;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import com.alibaba.fastjson.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.annotation.JmsListener;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;@Componentpublic class Consumer {@Autowiredprivate JavaMailSender javaMailSender;//幂等性@JmsListener(destination = "${my_queue}")public void receive(String msg) throws Exception {if(StringUtils.isEmpty(msg)) {return ;}//解析jsonJSONObject jsonObject = JSONObject.parseObject(msg);String userName = jsonObject.getString("userName");String email = jsonObject.getString("email");sendSimpleMail(email, userName);System.out.println("监听器接到msg:"+msg);}public void sendSimpleMail(String email , String userName) throws Exception{SimpleMailMessage message = new SimpleMailMessage();message.setFrom(email);message.setTo(email);message.setSubject("蚂蚁课堂|美特教育 新学员提醒...");message.setText("祝贺您,成为我们的新学员!!!");javaMailSender.send(message);System.out.println("成功发送邮件:"+JSONObject.toJSONString(message));}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值