title: How to include image in ftl template from java other than base64
body: <p>We have a requirement to send mail from java where I need to load an Ftl tempalte using freemarker.template.Configuration class which has an img src tag where I need to add an image from classpath . I tried directly giving resource path in img src tag but it didn't work. Then I found a solution to include an image using Base64 encoding. It worked in thunderbird but not in webmail, outlook and gmail (These mail service providers didn't support Base64 embedded image). I tried for some other approach in google but didn't got a proper solution. In case if I'm going with javaMailSender it can done with reference in below link </p>
  
  <p><a href="https://memorynotfound.com/spring-mail-sending-email-freemarker-html-template-example/" rel="nofollow noreferrer">https://memorynotfound.com/spring-mail-sending-email-freemarker-html-template-example/</a></p>
  
  <p>But for our requirement we need to give our mail body to some other service which will trigger the mail . So to say in Short how to include image in ftl template from java other than base64.</p>
  
  <pre><code>public class mailTrigger {  
  
      @Autowired
      private Configuration freemarkerConfig;
  
      private String frameMail(final String userName) throws Exception {
  
  
          final Template t = freemarkerConfig.getTemplate("mailTemplate.ftl");
  
  
          ClassPathResource res = new ClassPathResource("classpath:logo.jpg");    
              File img = new File(res.getPath());
  
          byte[] imgBytes = IOUtils.toByteArray(new FileInputStream(img));
          byte[] imgBytesAsBase64 = Base64.encodeBase64(imgBytes);
          String imgDataAsBase64 = new String(imgBytesAsBase64);
          String logo = "data:image/png;base64," + imgDataAsBase64;
  
  
          final Map&lt;String, Object&gt; model = new HashMap();
          model.put("userName", userName);
          model.put("Logo", Logo);
  
          final String message = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
  
          return message;
  
      }
  }
  </code></pre>