从本地路径将图像添加到电子邮件

Add image to an email from a local path

如何使用服务器内部的路径添加要包含在通过 Spring 使用 Thymeleaf 发送的电子邮件中的图像?

这是我的控制器:

final Map<String, String> inlineResources = new HashMap<String, String>();
Set<String> folderPath = new TreeSet<>();
  for (File file : files) {

   ... some previous code ()   
   
   StringBuilder pathFile = new StringBuilder(config.getFileCachePath());
   pathFile.append("/").append(folder.getId()).append("/").append(certificateFile.getName());
   folderPath.add(pathCertificate);
  }
    
    String folderPathString = StringUtils.collectionToCommaDelimitedString(folderPath);
    
    inlineResources.put("img1", "/template/email/img/myImg.png");
    inlineResources.put("file", "folderPath");

这是我的 html 电子邮件:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title th:remove="all">Title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body style="font-family:Trebuchet MS;">
     
     <p>TEXT EMAIL</p>
     <img style="max-width: 100%;" th:src="'cid:img1'" />
     <img style="max-width: 100%;" th:src="'cid:file'" />
    </body>
</html>

在这种情况下 img1 加载正确,而 th:src="'cid:file'" 未加载。

并在folderPath.add(pathCertificate)中;我有这个错误:

类型 Set 中的方法 add(String) 不适用于参数 (StringBuilder)

我该如何解决这个问题?

这个问题的解决方法是使用"file:":

String pathImg = certificateFile.toString().replace('\', '/'); inlineResources.put("img", "file:"+pathImg);