SpringBoot整合ActiveMQ实现Email发送:专业负责MQ20年~

简介: javax.mail依赖__javax.mail + mq javax.mail mail 1.
img_e245fd9e4b32a42b48b1ba3af5791047.png
javax.mail

依赖__javax.mail + mq

<!--javax.mail-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
<!--activeMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>

application.properties

#ActiveMQ
spring.activemq.broker-url=tcp://localhost:61616

场景:

用户注册时,需要给用户发送邮件,采用ActiveMQ作为消息中间件

Controller 中的 Producer

//注入JmsTemplate
@Autowired
    private JmsTemplate jmsTemplate;

 @RequestMapping("/register")
    public String register(HttpServletRequest request) throws MessagingException {
        String userName = request.getParameter("username");
        String passWord = request.getParameter("password");
        String email = request.getParameter("email");
        User user = new User();
        user.setUserName(userName);
        user.setPassWord(passWord);
        user.setEmail(email);
        user.setState("0");
        user.setCreateTime(new Date());
        userService.addUser(user);
       // MailUtils.sendMail(email,"欢迎您成为我们的会员");
        /**
         * MQ
         */
        jmsTemplate.send("mail_lp", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                MapMessage message = session.createMapMessage();
                message.setString("title","注册邮件");
                message.setString("email",email);
                return message;
            }
        });
        return "/success";
    }

Consumer

消费类上必须有 @Component 或者@Service 注解,这样Consumer才会委派给Listener.

@Component
public class Consumer {
    @JmsListener(destination = "mail_lp")
    public void sendMail(Message message) throws JMSException, MessagingException {
        MapMessage mm = (MapMessage) message;
        String title = mm.getString("title");
        String email = mm.getString("email");
        MailUtils.sendMail(email,title);
    }
}

MailUtils

public class MailUtils {

    public static void sendMail(String email, String emailMsg)
            throws AddressException, MessagingException {
        // 1.创建一个程序与邮件服务器会话对象 Session

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "SMTP");
        props.setProperty("mail.host", "smtp.126.com");
        props.setProperty("mail.smtp.auth", "true");// 指定验证为true

        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
             //return new PasswordAuthentication("CalistaAngel", "lp123456");给你们用我的问题也不大毕竟我不是那么小气的人哈哈哈哈哈
                return new PasswordAuthentication("邮箱名", "授权码");
            }
        };

        Session session = Session.getInstance(props, auth);

        // 2.创建一个Message,它相当于是邮件内容
        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress("CalistaAngel@126.com")); // 设置发送者

        message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者

        message.setSubject("用户注册");
        // message.setText("这是一封激活邮件,请<a href='#'>点击</a>");

        message.setContent(emailMsg, "text/html;charset=utf-8");

        // 3.创建 Transport用于将邮件发送

        Transport.send(message);
    }
}
img_ae781e59806376fe2d97781f5e34f2eb.png
mq
img_414d556f23134329bf419d4ec4ae09f0.png
Email
目录
相关文章
|
4月前
|
消息中间件 Java
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
14 0
|
4月前
|
消息中间件 Java Kafka
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
32 0
|
10月前
|
消息中间件 Java
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
83 0
|
10月前
|
消息中间件 Java
springboot整合ActiveMQ(点对点+发布订阅)
springboot整合ActiveMQ(点对点+发布订阅)
|
10月前
|
消息中间件 Java Kafka
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
131 0
|
消息中间件 Java Maven
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
95 0
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
|
消息中间件 网络协议 Java
SpringBoot 整合 ActiveMQ|学习笔记
快速学习 SpringBoot 整合 ActiveMQ
123 0
SpringBoot 整合 ActiveMQ|学习笔记
|
消息中间件 网络协议 Java
springboot整合jms之activemq
springboot整合jms之activemq
130 0
springboot整合jms之activemq
Java:SpringBoot集成JWT实现token验证
Java:SpringBoot集成JWT实现token验证
366 0
Java:SpringBoot集成JWT实现token验证
|
前端开发 小程序 安全
微信小程序+Springboot实现宠物医院管理系统
本项目基于微信小程序开发实现了宠物医院管理系统的前端页面,基于Springboot+Mybatis实现了宠物医院管理系统的后台系统,采用前后端分离开发的模式来开发实现。功能齐全,操作简洁,技术性完整,页面简洁大方。其中后台管理模块主要包含有: 资料管理:个人资料管理、宠物资料管理、公告管理、医院资料管理、职工资料管理、住院资料管理、病历资料管理 预约管理:挂号预约、洗美预约、挂号预约、洗美预约 记录管理:诊断记录、洗美记录、支付记录 登陆、退出、个人信息修改、修改密码等功能 前端主要包含的功能模块有: 用户在线预约挂号等 查看医院公告信息
339 0