maven引入包hutool以及发送email需要的包
<!--hutool发送邮件-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.6</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6</version>
</dependency>
新建controller方法
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.json.JSONObject;
import com.fgi.admin.modules.map.entity.BsmapXzqEntity;
import com.fgi.admin.modules.map.entity.MailConfig;
import com.fgi.common.common.utils.Result;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@RestController
@RequestMapping("sendEmail/")
public class SendEmailController {
protected static final Logger logger = LoggerFactory.getLogger(SendEmailController.class);
@Autowired
private MailConfig mailConfig;
@Autowired
ResourceLoader resourceLoader;
private final String PATH = "static\\mail\\mailTemplate.html";
@RequestMapping("/send")
public Result list(@RequestBody Map<String, String> params) {
String to = params.get("to");
String subject = params.get("subject");
MailAccount account = new MailAccount();
account.setHost(mailConfig.getHost());
account.setPort(mailConfig.getPort());
account.setAuth(mailConfig.isAuth());
account.setFrom(mailConfig.getFrom());
account.setUser(mailConfig.getFrom());
account.setPass(mailConfig.getPass());
account.setStarttlsEnable(mailConfig.isStarttlsEnable());
account.setSslEnable(mailConfig.isSslEnable());
account.setSocketFactoryClass(mailConfig.getSocketFactoryClass());
account.setSocketFactoryFallback(mailConfig.isSocketFactoryFallback());
account.setSocketFactoryPort(mailConfig.getSocketFactoryPort());
ClassPathResource resource = new ClassPathResource(PATH);
String result = null;
InputStream inputStream = null;
try {
inputStream = resource.getInputStream();
result = IOUtils.toString(inputStream, "utf-8");
}catch (Exception e){
return Result.failed("读取模版文件失败");
}finally {
IoUtil.close(inputStream);
}
Set<String> stringSet = params.keySet();
Iterator<String> iterator = stringSet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
result = result.replace("${" + key + "}", params.get(key));
}
MailUtil.send(account, CollUtil.newArrayList(to), subject, result, true);
return Result.ok();
}
}
配置email对象
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties("mail")
public class MailConfig {
/**
* 服务器地址
*/
private String host;
/**
* 端口,默认25
*/
private int port;
/**
* 默认true
*/
private boolean auth;
/**
* 发件人邮箱地址
*/
private String from;
/**
* 发件人的邮箱授权码
*/
private String pass;
//开启ssl
private boolean starttlsEnable;
private boolean sslEnable;
private String socketFactoryClass;
private boolean socketFactoryFallback;
private int socketFactoryPort;
}
html模版
<!DOCTYPE html>
<html lang = "en" >
<head >
<meta charset = "UTF-8" >
<title >Title</title >
</head >
<body >
<div style="width:100%;text-align:center">
${name}
</div>
</body>
html放在resource->static->mail文件夹下 路径在controller配置了
请求时候将参数传入name即可替换html中的对应${name},subject为发送主题,to为目标邮箱
参数json串模版
{
"name": "123",
"subject": "标题222",
"to": "454578219@qq.com"
}