2017-04-05 1 views
0

Comment puis-je ajouter le chemin d'accès à un serveur, une image à inclure dans un e-mail envoyé via Spring avec Thymeleaf?Ajouter une image à un e-mail à partir d'un chemin local

Ceci est mon contrôleur:

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

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

Et ce mon email 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;"> 
 
    \t  
 
    \t <p>TEXT EMAIL</p> 
 
    \t <img style="max-width: 100%;" th:src="'cid:img1'" /> 
 
    \t <img style="max-width: 100%;" th:src="'cid:file'" /> 
 
    </body> 
 
</html>

Dans ce cas, la charge img1 co rrect, alors que th: src = "'cid: file'" n'est pas load.

Et dans folderPath.add (pathCertificate); J'ai cette erreur:

La méthode add (String) dans le type d'ensemble est pas applicable pour les arguments (StringBuilder)

Comment puis-je résoudre le problème?

+0

Peut-être que 'file' n'est pas publique? – Pau

+0

On dirait que vous essayez de mettre un StringBuilder à l'ensemble au lieu d'un String. Appelez la méthode toString() sur StringBuilder. –

+0

Votre cheminCertificat est de type StringBuilder. J'ai essayé ce 'folderPath.add (pathCertificate.toString());'? – Lucky

Répondre

0

La solution à ce problème est d'utiliser "file:":

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