Skip to content

javayh mail starter 说明

杨海吉 edited this page Apr 5, 2020 · 2 revisions

javayh-mail-starter 说明

javayh-mail-starter 是项目邮件处理的核心依赖

使用方式

1. 依赖引入

 <dependency>
    <groupId>com.javayh</groupId>
    <artifactId>javayh-mail-starter</artifactId>
</dependency>

2. 开启邮件支持

在启动类添加 @EnableMail 开启对邮件的支持 在最新版本中,@EnableMail已经废弃,实现类自动注入

@EnableMail
@JavayhBootApplication
public class MailApplication {

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

3. 使用

3.1 发送文本邮件

@Autowired(required = false)
private MailSend mailSenderUtil;

@PostMapping(value = "send")
public ResultData send(@RequestBody MailDO mailDO){
    Map<String,Object> map = new HashMap<>();
    map.put("username","Yang haiji");
    mailDO.setAttachment(map
    mailSenderUtil.sendTextMail(mailDO);
   return ResultData.success();
}

3.2 发送模板邮件

@Autowired(required = false)
private MailSend mailSenderUtil;

@PostMapping(value = "send")
public ResultData send(@RequestBody MailDO mailDO){
    Map<String,Object> map = new HashMap<>();
    map.put("username","Yang haiji");
    mailDO.setAttachment(map);
    mailDO.setTemplateName("HelloMail");
    mailSenderUtil.sendTextMail(mailDO);
   return ResultData.success();
}

发送模板需要添加配置

thymeleaf:
  enabled: true
  mode: LEGACYHTML5
  encoding: UTF-8
  prefix: classpath:/templates/ # 模板存放在资源目录的 templates/ 文件下
  suffix: .html # 模板后缀
  check-template-location: true
  check-template: false
  cache: false # 调试时关闭缓存
  servlet:
    content-type: text/html

详细使用见,javayh-demo-mail