13

I am using the spring samples to send inline images. It works but gmail shows images also as attachments. How to avoid it?

enter image description here

The code is pretty simple.

public class Email {

    public static  MimeMessagePreparator getContentAsInlineResourceMessagePreparator(final String to) {

        MimeMessagePreparator preparator = new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

                helper.setSubject("Email with inline image");
                helper.setFrom("fake@yourshop.com");
                helper.setTo(to);

                String content = "Dear pedrofb...";
                helper.setText("<html><body><p>" + content + "</p><img src='cid:company-logo'></body></html>", true);
                helper.addInline("company-logo", new ClassPathResource("logo.png"));
            }
        };
        return preparator;
    }
    public final static void main (String argv[]){
        //Basic SMTP configuration
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);

        MimeMessagePreparator preparator = getContentAsInlineResourceMessagePreparator("myemail@gmail.com");            
        mailSender.send(preparator);
    }

}

My question is similar to How to stop embedded images in email being displayed as attachments by GMail? but the answer is very old and it does not show how to configure spring properly. I do not want to build the message parts&headers myself


Posted the raw message in pastebin

pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • The second parameter of `MimeMessageHelper` also takes code, https://docs.spring.io/autorepo/docs/spring/4.1.4.RELEASE/javadoc-api/index.html?org/springframework/mail/javamail/MimeMessageHelper.html. Please play around with all 4 modes and see if any of them help you with the gmail email. Also I would suggest to try with `companyLogo` instead of `company-logo` – Tarun Lalwani Apr 16 '18 at 09:50
  • @TarunLalwani thanks. No luck... – pedrofb Apr 16 '18 at 10:15
  • 1
    Please do a raw view of the email in gmail and create a pastebin of the same and share – Tarun Lalwani Apr 16 '18 at 10:23
  • @TarunLalwani, posted raw message https://pastebin.com/uiQMJUHQ – pedrofb Apr 16 '18 at 10:34

2 Answers2

12

The issue is with MimeType determination

MimeType

The png extension is treated as image/x-png instead of image/png this causes the issue with Gmail. This has been fixed/changed in 5.X and may also be in some 4.X later version also (I am not sure on those). But the fix is quite easy. Change

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

to

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8") {

    @Override
    public void addInline(String contentId, Resource resource) throws MessagingException {
        Assert.notNull(resource, "Resource must not be null");
        String contentType = this.getFileTypeMap().getContentType(resource.getFilename());
        contentType = contentType.replace("x-png", "png");
        this.addInline(contentId, resource, contentType);

    }
};

And it will override the MimeType to image/png

Inline image

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
0

I have executed your code. It works perfectly: I open the resulting email in gmail, I see the inline image, and I don't see any attachment! This may be due to the library version? I have used the 5.0.5.RELEASE.

If this is not the solution, I guess you may have some uncommon property in your SmtpServer.toJavaMailSender() or in your Gmail settings.

luca.vercelli
  • 898
  • 7
  • 24