无法使用 Thymeleaf templateEngine 准备带有内联图像的邮件?

Couldn't prepare mail with inline image using Thymeleaf templateEngine?

我正在尝试使用 spring mvc 和 Thymeleaf 发送一封附有内联图像的 html 电子邮件。我的代码无需附加图像即可工作。我成功收到 html 电子邮件,但没有图像。但是当我将以下行添加到我的代码时,我收到以下错误消息。

如果没有这行代码,我会收到没有图片的电子邮件:

 message.addInline(image.getName(), imageSource,  image.getContentType());

这是方法:

public void sendUserRegisterEmail(String receiver, String receiverEmailAddress){
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {

            Path path = Paths.get("/assets/dist/img/dialog_tv_logo-white-01.png");
            String fileName = "dialog_tv_logo-white-01.png";
            String originalFileName = "dialog_tv_logo-white-01.png";
            String contentType = "image/png";
            byte[] content = null;
            try {
                content = Files.readAllBytes(path);
            } catch (final IOException e) {
            }
            MultipartFile image = new MockMultipartFile(fileName,
                    originalFileName, contentType, content);

            Locale locale = Locale.getDefault();
            final Context ctx = new Context(locale);
            ctx.setVariable("name", receiver);
            ctx.setVariable("subscriptionDate", new Date());
            ctx.setVariable("imageResourceName", image.getName());

            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setSubject(USER_REGISTER_MESSAGE_SUBJECT);
            message.setTo(receiverEmailAddress);
            message.setFrom(SENDER_EMAIL_ADDRESS);
            final String htmlContent = emailTemplateEngine.process("email-inlineimage", ctx);
            message.setText(htmlContent, true /* isHtml */);

            final InputStreamSource imageSource = new ByteArrayResource(image.getBytes());
            message.addInline(image.getName(), imageSource,  image.getContentType());

           }
    };
    sendEmail(preparator);
}  

添加图像准备行时出现以下错误消息:

 org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.IllegalStateException: Not in multipart mode - create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.

任何人都可以找出这里的问题吗?

千辛万苦终于修复了错误。更改以下行解决了该问题。通过传递 true

创建 MimeMessageHelper

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);