title: java mail Base64 encoded string to image attachment
body: <p>I have a base64 encoded string which is posted using JSON into a Spring form.</p>
  
  <pre><code>data:image/png;base64,iVBORw0KGg......etc
  </code></pre>
  
  <p>I want to add this image as an attachment to an email. Attaching the file works fine, but it is just adding the base64 string as the attachment.</p>
  
  <p>I am using the following code to create the attachment part.</p>
  
  <pre><code>private MimeBodyPart addAttachment(final String fileName, final String fileContent) throws MessagingException {
  
      if (fileName == null || fileContent == null) {
          return null;
      }
  
      LOG.debug("addAttachment()");
  
      MimeBodyPart filePart = new MimeBodyPart();
  
      String data = fileContent;
      DataSource ds;
      ds = new ByteArrayDataSource(data.getBytes(), "image/*");
  
      // "image/*"
      filePart.setDataHandler(new DataHandler(ds));
      filePart.setFileName(fileName);
  
      LOG.debug("addAttachment success !");
  
      return filePart;
  
  }
  </code></pre>
  
  <p>I also tried </p>
  
  <pre><code> ds = new ByteArrayDataSource(data, "image/*");
  </code></pre>
  
  <p>How can I convert the base64 string into a proper image file using the ByteArrayDataSource ?</p>