35.邮件服务

邮件服务搭建

添加模块
模块名:mailserver
选择Spring Initializer,添加Thymeleaf、RabbitMQ、Java Mail Sender,Spring web就不需要添加了,只需要依赖之前的vhr-model就行
35.邮件服务_第1张图片

添加依赖

vhr/pom.xml添加mailserver模块


        vhrserver
        mailserver

mailserver/pom.xml


            org.javaboy
            vhr-model
            1.0-SNAPSHOT

开启邮箱的pop3/smtp服务
网易163邮箱
35.邮件服务_第2张图片

配置文件

server.port=8082
spring.mail.host=smtp.163.com
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.password=xxxxxxxxxxx
[email protected]
spring.mail.port=25
spring.mail.properties.mail.stmp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true

spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672

创建thymeleaf邮件模板




    
    入职欢迎邮件



欢迎  加入大家庭,您的入职信息如下:
姓名
职位
职称
部门

希望在未来的日子里,携手共进!

MailServerApplication

@SpringBootApplication
public class MailserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(MailserverApplication.class, args);
    }

    @Bean
    Queue queue(){
        return new Queue("com.welcome");
    }
}

创建receiver/MailReceiver

@Component
public class MailReceiver {
    public static final Logger logger = LoggerFactory.getLogger(MailReceiver.class);
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    MailProperties mailProperties;
    @Autowired
    TemplateEngine templateEngine;

    //RabbitMQ监听对象
    @RabbitListener(queues = "com.welcome")
    public void handler(Employee employee){
        logger.info(employee.toString());
        //收到消息,发送邮件
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg);
        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());
            //渲染HTML字符串
            String mail = templateEngine.process("mail", context);
            helper.setText(mail, true);// 正文
            javaMailSender.send(msg);//发送
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("邮件发送失败:" + e.getMessage());
        }
    }
}

给新入职员工发送欢迎邮件

vhr-model下的model/employee实现Serializable

public class Employee implements Serializable

employee中的以下成员实现Serializable

private Nation nation;
private Politicsstatus politicsstatus;
private Department department;
private JobLevel jobLevel;
private Position position;

vhr-web/application.properties添加消息中间件配置

spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672

Service

EmployeeService

 @Autowired
RabbitTemplate rabbitTemplate;

public final static Logger logger = LoggerFactory.getLogger(EmployeeService.class);
    
//在添加员工时发送邮件
public Integer addEmp(Employee employee) {
        //省略
        //添加如下代码
        int result = employeeMapper.insertSelective(employee);
        if (result == 1) {
        	//获取添加员工的id并将id转化为职称名称,部门名称等。
            Employee emp = employeeMapper.getEmployeeById(employee.getId());
            //打印职工信息,确认是不是消息中间件的问题
            logger.info(emp.toString());
            //对应模板中的监听的队列名称 
            rabbitTemplate.convertAndSend("javaboy.mail.welcome", emp);
        }
        return result;
    }

Mapper

EmployeeMapper

Employee getEmployeeById(Integer id);

XML





  

你可能感兴趣的:(35.邮件服务)