title: How to add image to a Freemarker template with JavaMail
body: <p>Now before this question gets marked as a duplicate, 
  <a href="https://stackoverflow.com/questions/9208027/add-image-to-freemarker-template-for-mail">Add image to freemarker template for mail?</a></p>
  
  <p>This question does not provide the Solution to my Problem. 
  So I want to add Image as a Background in a Freemarker HTML template which I am attaching to a Google Gmail API Program in Java. </p>
  
  <p>Here is the Code for the Freemarker:- </p>
  
  <pre><code>&lt;style&gt;
      body{
  background-image: url("cid:${image.jpg}");
  }
       .container {
                position: absolute;
                padding:25px;
                margin:25px;
                border:5px;
                text-align: justify;
                text-justify: inter-word;
                font-family: "Times New Roman";
                font-style: italic;
              }
   &lt;/style&gt;
  </code></pre>
  
  <p>And the Way I am Inserting an Image using the Google Gmail API is:- </p>
  
  <pre><code>Multipart multipart = new MimeMultipart();
      MimeBodyPart bodyPart = new MimeBodyPart();
      Template t = freemarkerTemlate.getTemplate("OneEmployee.ftl");
      String mailContent = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
      bodyPart.setContent(mailContent, "text/html");
      multipart.addBodyPart(bodyPart);
  
      DataSource fds = new FileDataSource(Mailer.class.getClassLoader().getResource("images/image1.jpg").getFile());
      bodyPart = new MimeBodyPart();
      bodyPart.setDataHandler(new DataHandler(fds));
      bodyPart.setHeader("Content-ID", "&lt;image.jpg&gt;");
      multipart.addBodyPart(bodyPart);
  </code></pre>
  
  <p>My Images are in the Resources Directory which contains another Image Directory </p>
  
  <p>Now I have tried almost all variations of the code possible such as using the </p>
  
  <pre><code>&lt;img src=""&gt;
  </code></pre>
  
  <p>Adding the cid: within the ${} and also without using the "" but it just does not seem to set the Image in any case. </p>
  
  <p>In some of the Cases it runs without attaching an Image and in some cases it runs but throws an Error on the Console also:- </p>
  
  <pre><code>Application failed at {image1.jpg}:- Missing or Null Argument 
  </code></pre>
  
  <p>The Model that I am using is:- </p>
  
  <pre><code> Map model = new HashMap();
   model.put("emailNames", emailNames);
  </code></pre>
  
  <p>I havent added anything else to the Model since this is the Only other thing apart from the Image that I am inserting into the freemarker template </p>
  
  <p>Is there anything that I am doing wrong or missing out on ?</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I have also tried inserting the Image via the Model by setting it as a Key 
  Code has been Attached for Reference</p>
  
  <pre><code>Map model = new HashMap();
   model.put("emailNames", emailNames);
   model.put("image.jpg", Mailer.class.getClassLoader().getResource("images/image1.jpg").getFile())
  </code></pre>
  
  <p>It Still fails to work!</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Interestingly I noticed that when I do not define <code>cid</code> then it does adds the background Image as an Attachment, but whenever I add the <code>cid</code> syntax it does not add as an Attachment but it simply ignores the mail. </p>
  
  <p>I read on the net that according to the tutorials you do not pass the Image to be inserted as a part of the model but rather add it as an Attachment defining the content_id (cid) of the Image. </p>
  
  <p>Thank You </p>