描述:實(shí)現(xiàn)一個正文帶表格的郵件發(fā)送功能欢搜,基于freemarker
技術(shù):springboot+freemarker+郵件
1.實(shí)現(xiàn)效果
fE8DRR8iSz.jpg
2.程序?qū)崿F(xiàn)
2.1項(xiàng)目結(jié)構(gòu)
圖片.png
2.2配置類
圖片.png
2.3模板
<!DOCTYPE html>
<html>
<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"/></head>
<table style=font-size:12px border=1 cellspacing=0 width=100% bordercolorlight=DarkGray bordercolordark=#efefef>
<tr><th>序號</th><th>編號</th><th>名字</th><th>詳情</th></tr>
<#if faultOptimizationPlans1?? && (faultOptimizationPlans1?size>0)>
<#list faultOptimizationPlans1 as item>
<tr height=""18>
<td class="xl63">${(item.index)!}</td>
<td class="xl63">${(item.id)!}</td>
<td class="xl63">${(item.name)!}</td>
<td class="xl63">${(item.remark)!}</td>
</tr>
</#list>
</#if>
</table>
<br>
</html>
2.4郵件服務(wù)
2.4.1domain
public class Row {
private String index;
private String id;
private String name;
private String remark;
public Row(String index, String id, String name, String remark) {
this.index = index;
this.id = id;
this.name = name;
this.remark = remark;
}
public String getIndex() {
return index;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getRemark() {
return remark;
}
}
2.4.2dao
public interface IMailDAO {
boolean sendTemplateMail(String title, String toUser, String templateName, Map<String, Object> params);
}
2.4.3service
public interface MailService {
boolean sendTemplateMail(String title, String toUser, String templateName, Map<String, Object> params);
}
2.4.4impl
import cn.com.crv.event.service.service.MailService;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
@Slf4j
@Service
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Value("${spring.mail.host}")
private String host;
@Override
public boolean sendTemplateMail(String title, String toUser, String templateName, Map<String, Object> params) {
try {
Properties prop = new Properties();
prop.setProperty("mail.debug", "true");
prop.setProperty("mail.host", host);
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(prop);
MimeMessage mimeMessage = new MimeMessage(session);
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.name());
// 發(fā)件人
helper.setFrom(from);
//收件人
helper.setTo(toUser);
//郵件標(biāo)題
helper.setSubject(title);
Configuration configuration = new Configuration();
configuration.setClassForTemplateLoading(this.getClass(), "/templates");
String text = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(templateName), params);
//// text:內(nèi)容,true:為HTML郵件(false則為普通文本郵件)
helper.setText(text, true);
mailSender.send(mimeMessage);
log.info("郵件發(fā)送成功");
} catch (Exception e) {
log.error("sendTemplateMail 發(fā)送模板郵件錯誤键兜,", e);
throw new RuntimeException(e);
}
return true;
}
}
2.5測試類
import cn.com.crv.event.service.domain.Row;
import cn.com.crv.event.service.service.MailService;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.*;
import java.util.*;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes={Application.class})
@Slf4j
public class EmailTest {
@Autowired
private MailService mailService;
@Test
public void sendTemplateMailTest() throws IOException, TemplateException {
String subject = "這是一封測試郵件";
List<Row> rows = new ArrayList<>();
rows.add(new Row("1", "A1", "Name1", "Remark1"));
rows.add(new Row("2", "A2", "Name2", "Remark2"));
rows.add(new Row("3", "A3", "Name3", "Remark3"));
Map<String,Object> mailContentMap = new HashMap<>();
mailContentMap.put("faultOptimizationPlans1", rows);
try {
mailService.sendTemplateMail(subject, "接受收者的郵箱地址","normal.ftl", mailContentMap);
} catch (Exception e) {
log.error("郵件發(fā)送失斢簟:{}", mailContentMap , e);
}
}
2.6相關(guān)依賴
<!--spring email -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- 郵箱發(fā)送依賴-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>