Spring整合Freemarker实现邮件发送服务
最近项目中有发送邮件需求,使用Spring邮件服务实现,由于第一次使用,所以自己编写一个测试demo,并做记录,以下为具体配置及代码。
1. 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. application.properties配置
由于项目中使用的为公司邮箱服务器,password即为用户名密码,如何开启smtp授权服务获取授权码可自行百度,挺简单的。
#用户名
spring.mail.username=xxxxx@163.com
#授权密码或用户名密码
spring.mail.password=xxxxxxxxxx
#主机名
spring.mail.host=smtp.163.com
spring.mail.properties.mail.smtp.ssl.enable=true
3. 邮件发送服务代码
@Service
public class EmailService {
@Autowired
private JavaMailSenderImpl javaMailSender;
@Autowired
private FreeMarkerCmp freeMarkerCmp;
/**
* 发送普通邮件
*/
public void sendSimple() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("email测试");
mailMessage.setText("email测试内容");
mailMessage.setTo("xxxxx@foxmail.com");
mailMessage.setFrom("xxxxx@163.com");
javaMailSender.send(mailMessage);
}
/**
* 发送复杂邮件
* @throws MessagingException
*/
public void sendComplicated() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//true代表支持多组件,如附件,图片等
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setSubject("Email测试");
//true代表支持html,包含html标签
mimeMessageHelper.setText("email测试内容", true);
mimeMessageHelper.setTo("xxxxx@foxmail.com");
mimeMessageHelper.setFrom("xxxxx@163.com");
/** 添加附件,可多次调用添加多个附件 */
mimeMessageHelper.addAttachment("pic.jpg", new File("C:\\Pictures\\Saved Pictures\\pic.jpg"));
javaMailSender.send(mimeMessage);
}
/**
* 发送带模板邮件
* @throws MessagingException
*/
public void sendHtml() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
String content = freeMarkerCmp.generateTemplateByString();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setSubject("Email测试");
mimeMessageHelper.setText(content, true);
mimeMessageHelper.setTo("xxxxx@foxmail.com");
mimeMessageHelper.setFrom("xxxxx@163.com");
javaMailSender.send(mimeMessage);
}
}
4. FreeMarker组件
@Slf4j
@Component
public class FreeMarkerCmp {
public String generateTemplateByString() {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
String templateString = "<html>\n" +
"<body>\n" +
"\n" +
"<h1>Email Test</h1>\n" +
"\n" +
"<p>${name}</p>\n" +
"\n" +
"</body>\n" +
"</html>";
//创建模板加载器
StringTemplateLoader templateLoader = new StringTemplateLoader();
//存入模板
templateLoader.putTemplate("template", templateString);
//加载模板加载器
configuration.setTemplateLoader(templateLoader);
Template template = null;
try {
template = configuration.getTemplate("template", "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
Map<String, Object> map = new HashMap<>();
map.put("name", "Tawalt");
String content = null;
try {
content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
log.info("template:[{}]", content);
return content;
}
}
由于项目中html模板是从数据库中读取,所以需要StringTemplateLoader来加载模板内容;若是通过模板文件获取模板,则使用Configuration类加载模板,方式如下:
configuration.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(), "templates");
//模板文件放在templates目录下,templateName为模板文件文件名
Template template = configuration.getTemplate("templateName");
5. 测试类
@SpringBootTest
class MailTestApplicationTests {
@Autowired
EmailService emailService;
@Test
void sendSimpleEmail() {
emailService.sendSimple();
}
@Test
void sendComplicatedEmail() {
try {
emailService.sendComplicated();
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Test
void sendHtmlEmail() {
try {
emailService.sendHtml();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}