云E办项目--发送邮件

1.建立新的module

2.利用RabbitMQ实现邮件发送

(1)引入配置

server:
  port: 8082

spring:
  # 邮件配置
  mail:
    host: smtp.163.com
    protocol: smtp
    default-encoding: UTF-8
    password: #######
    username: #######
    port: 25
  rabbitmq:
    username: guest
    password: #####
    host: 39.102.65.157
    port: 5672
    listener:
      simple:
        acknowledge-mode: manual
  redis:
    timeout: 10000
    host: 127.0.0.1
    port: 6379
    database: 0 # 选择哪个库,默认0库
    lettuce:
      pool:
        max-active: 1024 # 最大连接数,默认 8
        max-wait: 10000ms # 最大连接阻塞等待时间,单位毫秒,默认 -1
        max-idle: 200 # 最大空闲连接,默认 8
        min-idle: 5
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>yeb</artifactId>
        <groupId>com.study</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>yeb-mail</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>com.study</groupId>
            <artifactId>yeb-server</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!--<scope>compile</scope>-->
        </dependency>

    </dependencies>

    <!--<build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>-->

</project>

(2)编写邮件模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>入职欢迎邮件</title>
</head>
<body>
    欢迎 <span th:text="${name}"></span> 加入 XXXX 大家庭,您的入职信息如下:
    <table border="1">
        <tr>
            <td>姓名</td>
            <td th:text="${name}"></td>
        </tr>
        <tr>
            <td>职位</td>
            <td th:text="${posName}"></td>
        </tr>
        <tr>
            <td>职称</td>
            <td th:text="${joblevelName}"></td>
        </tr>
        <tr>
            <td>部门</td>
            <td th:text="${departmentName}"></td>
        </tr>
    </table>
    <p>我们公司的工作忠旨是严格,创新,诚信,您的加入将为我们带来新鲜的血液,带来创新的思维,以及为
        我们树立良好的公司形象!希望在以后的工作中我们能够齐心协力,与时俱进,团结协作!同时也祝您在本公
        司,工作愉快,实现自己的人生价值!希望在未来的日子里,携手共进!</p>
</body>
</html>

(3)编写消息接受者

package com.study.mail;

import com.study.server.pojo.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


import java.util.Date;


/***
 * 消息接受者
 */
@Component
public class MailReceiver {
    private static final Logger LOGGER=  LoggerFactory.getLogger(MailReceiver.class);

    @Resource
    private JavaMailSender javaMailSender;
    @Resource
    private MailProperties mailProperties;
    @Resource
    private TemplateEngine templateEngine;

    @RabbitListener(queues = "mail.welcome")
    public  void handle(Employee employee){
        MimeMessage mimeMessage=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
        try {
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(employee.getEmail());
            helper.setSubject("入职邮件");
            helper.setSentDate(new Date());
            //邮件内容
            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());
            //生成mail
            String mail = templateEngine.process("mail", context);
            helper.setText(mail, true);
            //发送mail
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            LOGGER.error("邮件发送失败",e.getMessage());
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值