緣起
日前,客戶需要項目中有發送郵件功能,並且給了一個郵件模板是excel的
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-XSIB1dUh-1587863263542)(http://www.bxoon.com/upload/2020/4/image-91f217e55d5c47e1a6f7025ea99caa1e.png)]
需要在郵件正文中按照這個模板顯示出來,經過一般折騰,最後實現了這個功能,效果如下
可以看到是把excel模板內容插入到了郵件的正文中,那麼這是如何實現的呢?
實現思路
在java中發送郵件,是可以採用模板功能的。大概思路也就是定義好一個模板,然後再模板的對應位置打上標記,再用代碼給這些標記賦值就行。
模板類型以ftl結尾,一個可用的模板類似如下
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-UHbAMvYo-1587863263549)(http://www.bxoon.com/upload/2020/4/image-0d6dc3c024d1443b873e543bd6d64ff0.png)]
可用看到,模板基本上就是一個html形式,那麼下一步就是思考如何把一個excel轉成html文件,這裏可以用excel的另存爲功能
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-131Dr0zY-1587863263553)(http://www.bxoon.com/upload/2020/4/image-f22deaa159374b3b9d0a39e34add4cb0.png)]
另存爲之後,可以在桌面看到html文件和css文件以及一些圖片文件,這裏html文件中引用了CSS,圖片等,但是模板中好像是不能通過相對路徑去引用這些文件的,需要採用絕對路徑。這裏有兩種解決方案
- 把css中的內容全部copy到html文件中,然後統一copy到ftl模板文件中
- 通過在ftl文件中引用路徑的位置定義標籤,通過java代碼的方式動態設置請求的絕對路徑
我這裏兩種方式都採用了,css中的內容是全部copy到html文件中的,然後圖片是路徑是通過java代碼動態設置的。
- 這裏把css copy到html中保存的時候可能會提示你字符集不匹配,需要把一些看不懂的字符,類似mso-style-name:³£¹æ;這種給刪掉
- 關於如何在java代碼中動態設置圖片的請求絕對路徑可以採用如下方式,
-
- 先在模板中定義標籤
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-oH2jOfbK-1587863263555)(http://www.bxoon.com/upload/2020/4/image-27b055a6b81f41bca3b6e05b35aed584.png)]
- 先在模板中定義標籤
-
- 然後在java代碼中獲取項目的請求絕對路徑,然後給這個標籤賦值
- 然後在java代碼中獲取項目的請求絕對路徑,然後給這個標籤賦值
相關實現代碼
pom.xml配置freemarker
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
配置文件中配置springboot郵件發送支持配置
發送類
public void send(Message message) {
try {
//建立郵件消息
MimeMessage mainMessage = jms.createMimeMessage();
//用MimeMessageHelper組裝複雜郵件,第二個參數爲true,可以發送附件
MimeMessageHelper helper = new MimeMessageHelper(mainMessage, true);
FhdTemplateDto fhdTemplateDto = (FhdTemplateDto) message.getContent();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
StringBuilder urlSb = new StringBuilder(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/");
if (!StringUtils.isEmpty(request.getContextPath())) {
urlSb.append(request.getContextPath()).append("/");
}
urlSb.append("system/images/company_logo.png");
fhdTemplateDto.setLogo("<img src=" + urlSb.toString() + " height=\"81\" width=\"133\">");
Template template = freeMarkerConfigurationFactory.createConfiguration().getTemplate("emailTemplates/FhdMail.ftl");
String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, fhdTemplateDto);
helper.setFrom(mailusername);
helper.setTo(message.getAddressee());
helper.setSubject(message.getTitle());
helper.setText(text, true);
//添加附件
// helper.addAttachment(message.getAttachmentFileName(), new File(message.getAttachmentFilePath()));
jms.send(mainMessage);
log.debug("發送成功");
} catch (Exception e) {
log.debug("發送失敗", e);
}
}
Message
@Data
public class Message {
/**
* 消息標題
*/
private String title;
/**
* 收信人
*/
private String addressee;
/**
* 內容
*/
private Object content;
/**
* 附件名稱
*/
private String attachmentFileName;
/**
* 附件路徑
*/
private String attachmentFilePath;
}
模板文件(隱私問題不列出代碼)
使用
參考:
https://blog.csdn.net/qq_40205116/article/details/83932316
https://blog.csdn.net/qq_31281189/article/details/97760951