title: JavaMail - inlined images are also being attached to the email
body: <p>I have added inline images in my mail but it's getting attached with mail as well. My code:</p>
  
  <pre><code>public Map&lt;String, String&gt; sendMail(final String campaignId, final String emailTo[], final String emailSubject, final String emailBody, final MultipartFile[] mailAttachment) {
  
      Map&lt;String, String&gt; messageMailIdMap = new HashMap&lt;String, String&gt;();
      SMTPMessage SMTPMessageArray[] = new SMTPMessage[emailTo.length];
      try {
          MimeMessage mimeMessage = mailSender.createMimeMessage();
          MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
          messageHelper.getMimeMessage().setHeader("X-Priority", "High");
          messageHelper.getMimeMessage().setHeader("Return-Receipt-To", AppProperties.getInstance().getEmailId());
          messageHelper.getMimeMessage().setHeader("Disposition-Notification-To", AppProperties.getInstance().getEmailId());
          messageHelper.setFrom(AppProperties.getInstance().getEmailId(), AppProperties.getInstance().getUserNameAlias());
          messageHelper.setSubject(emailSubject);
  
          if (mailAttachment != null)
              for (final MultipartFile multipartFile : mailAttachment) {
                  if (multipartFile.getSize() != 0) {
                      String attachName = multipartFile.getOriginalFilename();
  
                      messageHelper.addAttachment(attachName, new InputStreamSource() {
  
                          @Override
                          public InputStream getInputStream() throws IOException {
                              return multipartFile.getInputStream();
                          }
                      });
                  }
              }
  
          for (int i = 0; i &lt; emailTo.length; i++) {
              messageHelper.setTo(emailTo[i]);
  
              if (emailBody != null &amp;&amp; emailBody.contains("&lt;img")) {
                  UtilityClass.saveEmailInlineImagesInTemp(emailBody);
                  messageHelper.setText(UtilityClass.appendUnsubcribeTextUrl(UtilityClass.setInlineTextWithImages(emailBody),unsubcribeTextUrl(emailTo[i],campaignId)), true);
  
                  int occurance = StringUtils.countOccurrencesOf(emailBody, "&lt;img");
                  for (int j = 0; j &lt; occurance; j++) {
                      File inlineImage = new File(UtilityClass.mailTempFolderPath() + System.getProperty("file.separator") + j + "mailInlineImage.png");
                      messageHelper.addInline(j + "mailInlineImage", inlineImage);
                  }
              } else
                  messageHelper.setText(UtilityClass.appendUnsubcribeTextUrl(emailBody.toString(),unsubcribeTextUrl(emailTo[i],campaignId)), true);// //Mail
              messageHelper.getMimeMessage().saveChanges();
  
              messageMailIdMap.put(emailTo[i], messageHelper.getMimeMessage().getMessageID());
  
              SMTPMessage smtpMessage = new SMTPMessage(messageHelper.getMimeMessage());
              smtpMessage.setReturnOption(SMTPMessage.RETURN_FULL);
              smtpMessage.setNotifyOptions(SMTPMessage.NOTIFY_DELAY | SMTPMessage.NOTIFY_FAILURE | SMTPMessage.NOTIFY_SUCCESS);
              SMTPMessageArray[i] = smtpMessage;
          }
  
          mailSender.send(SMTPMessageArray);
      } catch (Exception ex) {
          System.out.println("Error Sending!!!");
          ex.printStackTrace();
          return null;
      } finally {
          File mailTempFolder = new File(UtilityClass.mailTempFolderPath());
          if (mailTempFolder.exists()) {
              for (File file : mailTempFolder.listFiles())
                  file.delete();
              System.out.println("tempFilesDeleted........");
          }
      }
  
      return messageMailIdMap;
  }
  </code></pre>
  
  <p><code>UtilityClass.saveEmailInlineImagesInTemp(emailBody)</code> collects image info from html img tag and creates an image files.</p>
  
  <p><code>UtilityClass.setInlineTextWithImages(emailBody)</code> creates message body with img tags like <code>&lt;img src="cid:0mailInlineImage"&gt;</code></p>
  
  <p><code>messageHelper.addInline(j + "mailInlineImage", inlineImage);</code> adds inline images</p>
  
  <p><code>SMTPMessageArray[i] = smtpMessage;</code> to create an array of mails to send to multiple recipients with same session. I'm using Spring JavaMailSender to create MimeMessage.</p>
  
  <p>Log from <code>System.out</code>: </p>
  
  <pre><code>From: XXXXXXXXXXXXXXXXXXXXXXXXXXX
  To: XXXXXXXXXXXXXXXXXXXXXXXXXXX
  Message-ID: XXXXXXXXXXXXXXXXXXXXXXXXXXX
  Subject: bellaward
  MIME-Version: 1.0
  Content-Type: multipart/mixed; 
      boundary="----=_Part_16_711673112.1452149784499"
  X-Priority: High
  Return-Receipt-To: XXXXXXXXXXXXXXXXXXXXXXXXXXX
  Disposition-Notification-To: XXXXXXXXXXXXXXXXXXXXXXXXXXX
  
  ------=_Part_16_711673112.1452149784499
  Content-Type: multipart/related; 
      boundary="----=_Part_17_1410900889.1452149784499"
  
  ------=_Part_17_1410900889.1452149784499
  Content-Type: text/html;charset=UTF-8
  Content-Transfer-Encoding: 7bit
  
  &lt;p&gt;Hi &lt;/p&gt;&lt;p&gt;How Are you&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;img src='cid:0mailInlineImage'&gt;&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Thanks&lt;br&gt;&lt;/p&gt;
  ------=_Part_17_1410900889.1452149784499
  Content-Type: image/x-png
  Content-Transfer-Encoding: base64
  Content-Disposition: inline
  Content-ID: &lt;0mailInlineImage&gt;
  
  iVBORw0KGgoAAAANSUhEUgAAAUAAAACvCAIAAAADl5AeAAB0yUlEQVR42uy9h39c1bX3/fwnz+d9
  npvgIttyg5Qnt+Tm3hBIw8aqM2ouQAgBLsm9SSCh2OqSMSWFjrFVpo96l1yxZbXpfdR718yc3t61
  zpEnwtYYy0iWDDP8PkIanzlzzj77u9dae6+99//aU9QUV1xxPaD6X/EiiCuuOMBNuwsb44orrvus9++rAvgbIf8f/DPHTHrGDe4AAAAASUVORK5CYII=
  ------=_Part_17_1410900889.1452149784499--
  
  ------=_Part_16_711673112.1452149784499--
  </code></pre>