2017-04-21 4 views
1

SpringでThymeleafを介して送信される電子メールに、自分のPCまたはサーバーのローカルパスを使用してイメージを追加するにはどうすればよいですか?スプリング電子メールにインラインイメージを追加する

これは私のcontrollerMail.javaです:

final Map<String, String> inlineResources = new HashMap<String, String>(); 
Set<String> folderPath = new TreeSet<>(); 
     for (File file : files) { 
      certificateFile = file.getCertificateFile(); 
      String img = certificateFile.toString(); 
     inlineResources.put("file", img); 
     } 

    inlineResources.put("img1", "/template/email/img/myImg.png"); 

そして、この私の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> 

このパスを返すcertificateFile:/ SRVの/ dev /内容/ jpgCache /証明書/ 10000 /は、certificatename .jpg

私のmail.htmlは私のプロジェクトのsrc/main/resources/template/emailにあります。この場合、img1は正しい(同じパス/テンプレート/ email/imgにあります)が、このログエラーを返します:

無効なリソース:/ srv/dev/contents/jpgCache/certificate/10000 /certificateName.jpg

失敗したメッセージ:javax.mail.MessagingException:メッセージの送信中にIOExceptionが発生しました。 ネストされた例外は次のとおりです。 java.io.FileNotFoundException:それは私がこの問題を解決するにはどうすればよい

存在しないため、クラスパスリソースが[/srv/dev/contents/jpgCache/certificate/10000/certificateName.jpg]を開くことができません問題?

このファイルを電子メールに添付しても正しく動作しますが、 「pathImg」の前に:

+1

あなたは春のドキュメントを読むことがありますか? 'addInline()'メソッドを持つ 'MimeMessageHelper'クラスがあります。 –

+0

これを試してみたら:String certificato = new ClassPathResource(certificateFile.toString()); inlineResources.put( "certificato"、certificato);私はタイプミスマッチにエラーがあります。どうすれば問題を解決できますか? –

+0

私を助けることができますか? dev 12:46:20 DEBUG it.project.web.SinEmailService - :ログデバッグクラスのパスリソース[/srv/dev/contents/jpgCache/certificate/10000/certificateName.jpg]は正しいですか? –

答えて

0

この問題の解決策は、「ファイル」:使用することです

String pathImg = certificateFile.toString().replace('\\', '/'); inlineResources.put("img", "file:"+pathImg); 
関連する問題