无法使用Spring javamail在电子邮件中发送图像

时间:2017-09-06 07:48:36

标签: java spring javamail

使用spring 4.0.8

这是我的代码

@Value("classpath:logo.png")
private Resource logoImage;

...

final MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true,"UTF-8");
                message.setTo(toEmail);
                message.setSubject("subject");
                message.setFrom(from);
                message.setReplyTo(replyTo);
                message.addInline("logo.png", logoImage);
                message.setText("<img src=\"cid:logo.png\"></img><div>My logo</div>", true);
            }
        };
        mailSender.send(preparator);

我收到没有图片的电子邮件。

我调试时logoImage.exists() = true.

我做错了什么?

UPD: message.getMimeMessage().writeTo(System.out)打印此内容:

    From: myaddress@sh.com
    Reply-To: no-reply@sh.com
    To: kos@inbox.ru
    Message-ID: <2080132943.21504685477046.JavaMail.kn@kn>
    Subject: subject
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
    boundary="----=_Part_1_1016272857.1504685465423"

    ------=_Part_1_1016272857.1504685465423
    Content-Type: multipart/related;
    boundary="----=_Part_2_1648931910.1504685465435"

    ------=_Part_2_1648931910.1504685465435
    Content-Type: text/html;charset=UTF-8
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline
    Content-ID: <logo.png>

<img src="cid:logo.png"></img><div>My logo</div>
    ------=_Part_2_1648931910.1504685465435--

    ------=_Part_1_1016272857.1504685465423--

添加时会收到一条消息,我的帖子主要是代码,所以这里有些文字不介意:)

1 个答案:

答案 0 :(得分:2)

查看example。我认为你应该交换方法setTextaddInline