简体   繁体   中英

Can't send image in email with Spring javamail

Using spring 4.0.8

Here is my code

@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);

And I receive email without image.

When I debug it logoImage.exists() = true.

What I do wrong?

UPD: message.getMimeMessage().writeTo(System.out) print this:

    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--

when added this get a message, that my post has mostly code so here is some text don't mind it :)

Look at the example . I think you should swap the methods setText and addInline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM