Skip to content
Advertisement

How to prevent inline images from being converted to binary attachments in Microsoft Outlook?

My Java application creates an e-mail and sends it to a user. If the e-mail is sent to a Gmail account and the user checks his e-mail online in a web browser, the image that is in the body and at the bottom of the e-mail (i.e. not an attachment) will be displayed. However, if the e-mail is sent to a user who checks his e-mail via Microsoft Outlook (i.e. the e-mail passes through a Microsoft Exchange Server), the image is converted to a binary file and then attached to the e-mail, and at the bottom of the e-mail, the filename “ATT00001.bin” is displayed. How do I fix this issue so that the image will be displayed?

Here is what the e-mail should look like with the image, which should be at the bottom of the e-mail.

enter image description here

But here is how the e-mail looks in Microsoft Outlook. There is no image, only a binary file.

enter image description here

Here is my code for generating an e-mail with an inline image.

  MimeMessage message = mailSender.createMimeMessage();
  message.setSubject("Subject");

  MimeMessageHelper msg = new MimeMessageHelper(message, true);

  msg.setTo("recipient@gmail.com");
  msg.setFrom("sender@gmail.com");
  msg.setSentDate(new Date());

  Multipart multipart = new MimeMultipart();

  MimeBodyPart messageBodyPart = new MimeBodyPart();
  messageBodyPart.setContent("<html><body>Test<br />"
    + "<img src='cid:101@gmail.com'/></body></html>", "text/html");
  multipart.addBodyPart(messageBodyPart);

  MimeBodyPart imagePart = new MimeBodyPart();
  DataSource imgSrc = new FileDataSource(new File(getClass().getClassLoader()
    .getResource("images/Energy_Office-logo.png").toURI()));
  imagePart.setDataHandler(new DataHandler(imgSrc));
  imagePart.setContentID("<101@gmail.com>");
  // The following code does not fix the issue either.
  // imagePart.setHeader("Content-ID", "<" + "101@gmail.com" + ">");
  imagePart.setDisposition(MimeBodyPart.INLINE);
  multipart.addBodyPart(imagePart);

  MimeBodyPart pdfPart = new MimeBodyPart();
  DataSource pdfSrc = new ByteArrayDataSource(attachment, mime);
  pdfPart.setDataHandler(new DataHandler(pdfSrc));
  pdfPart.setFileName("file.pdf");
  multipart.addBodyPart(pdfPart);

  message.setContent(multipart, "text/html");

  message.saveChanges();

  mailSender.send(message);

Advertisement

Answer

I wrote the following code, and it solved my problem. The difference between this one and the one above is that I called setText instead of setContent for the body text, and I called attachFile instead of setDataHandler for the image.

private void send(String toAddress, String subject, String text, byte[] attachment) {
  JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
  mailSender.setHost(mailHost);

  MimeMessage message = mailSender.createMimeMessage();
  MimeMessageHelper msgHelper = new MimeMessageHelper(message, true);
  msgHelper.setSubject(subject);
  msgHelper.setFrom(fromAddress);
  msgHelper.setTo(toAddress);

  Multipart multipart = new MimeMultipart();

  MimeBodyPart messageBodyPart = new MimeBodyPart();
  messageBodyPart.setText(text, "UTF-8", "html");
  multipart.addBodyPart(messageBodyPart);

  MimeBodyPart imagePart = new MimeBodyPart();
  imagePart.attachFile(new File(PdfMailServiceImpl.class.getClassLoader().getResource("images/Energy_Office-logo.png").toURI()));
  imagePart.setContentID("<" + "101" + ">");
  imagePart.setDisposition(MimeBodyPart.INLINE);
  multipart.addBodyPart(imagePart);

  MimeBodyPart pdfPart = new MimeBodyPart();
  DataSource pdfSrc = new ByteArrayDataSource(attachment, mime);
  pdfPart.setDataHandler(new DataHandler(pdfSrc));
  pdfPart.setFileName(formFilename);
  multipart.addBodyPart(pdfPart);

  message.setContent(multipart, "text/html");

  message.saveChanges();

  mailSender.send(message);
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement