springboot+邮件+amqp消息队列

maven依赖

<dependencies>
<!--       消息队列-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
<!--        邮件starter-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>



    </dependencies>

application配置

spring:
  mail:
    # 邮件服务器地址
    host: smtp.163.com
    # 协议
    protocol: smtp
    # 编码格式
    default-encoding: utf-8
    # 授权码
    password: 
    # 邮箱
    username: 
    # 端口(邮箱网站可查,不同邮箱端口号不同)
    port: 25

  rabbitmq:
    username: guest
    password: guest
    host: #rabbitmq服务器地址
    port: 5672

生产者

/**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public RespBean addEmp(Employee employee) {
        // 处理合同期限
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long day = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(day/365)));
        int insert = employeeMapper.insert(employee);
        if(insert == 1){
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
            rabbitTemplate.convertAndSend("mail.welcome",emp);

            return RespBean.success("插入成功");
        }
        return RespBean.error("插入失败");
    }

消费者

@RabbitListener(queues = "mail.welcome")
    public void handler(Employee employee){

        MimeMessage mimeMessage =
                javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);

        try {
            // 发件人
            mimeMessageHelper.setFrom(mailProperties.getUsername());

            // 收件人
            mimeMessageHelper.setTo(employee.getEmail());

            // 发送日期
            mimeMessageHelper.setSubject("入职欢迎邮件");

            // 邮件内容
            Context context = new Context();
            context.setVariable("name",employee.getName());
            context.setVariable("posName",employee.getPosition().getName());
            context.setVariable("joblevelName",employee.getJoblevel().getName());
            context.setVariable("departmentName",employee.getDepartment().getName());
            String mail = templateEngine.process("mail", context);
            mimeMessageHelper.setText(mail);
            javaMailSender.send(mimeMessage);


        } catch (MessagingException e) {
            log.error("邮件发送失败");
            e.printStackTrace();
        }


    }
上一篇:【资讯类系统开发经验分享】——SpringBoot+Vue.js汽车资讯论坛系统 汽车资讯分享平台系统 汽车资讯发布平台系统(可视化+在线点播+JavaMail)


下一篇:使用公司邮箱服务器免密码配置