Embedding attached images in HTML emails

后端 未结 7 1584
既然无缘
既然无缘 2020-12-05 08:15

If I attach an image to an email, how can I place it in the HTML content? I tried just using the filename as the image source but that doesn\'t seem to work.

相关标签:
7条回答
  • 2020-12-05 08:18

    The image attachment section needs Content-ID

    --T4nu9J8b
    Content-Type: image/png
    Content-ID: <idname>
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment ;filename="testimage.png"
    
    iVBORw0KGgoAAAANS...
    --T4nu9J8b--
    

    Note: Content-ID name should be put in an angular bracket, as given

    Embed it into the tag using same Content-ID (without the angular bracket)

    <img alt="Embedded Image" src="cid:idname"/>
    

    This should allow the attached image to be displayed into the HTML!

    0 讨论(0)
  • 2020-12-05 08:26

    Be more specific on how you build the HTML mail message.

    The result will be a multipart-MIME message with a text/html part (if you really do it right with an alternate part of type text/plain) and several images, which are then referenced from within the HTML.

    See RFC 1813 and RFC 2378 for more information about content-id in mixed MIME and related data (referred by CID in the HTML source).

    0 讨论(0)
  • 2020-12-05 08:26

    I'm doing it in this way

    _mime = new MimeMultipart();
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart = new MimeBodyPart();
    DataSource fds = new FileDataSource("C:\\bgBoletin.jpg");
    
    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.setHeader("Content-ID", "<bgBoletin>");
    _mime.addBodyPart(messageBodyPart);
    

    And in HTM file whe reference de Content-ID in this way background: url(cid:bgBoletin).

    0 讨论(0)
  • 2020-12-05 08:26

    If this has to do with sending emails from a website you are developing, just keep the images on your server and then link to them using the <img src="url"/>.

    0 讨论(0)
  • 2020-12-05 08:33

    You can use the same way with src="cid:bgBoletin" property of img tag.

    0 讨论(0)
  • 2020-12-05 08:36

    The answer to your question is in the spring docs here.

     mailSender.send(new MimeMessagePreparator() {
       public void prepare(MimeMessage mimeMessage) throws MessagingException {
         MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
         message.setFrom("me@mail.com");
         message.setTo("you@mail.com");
         message.setSubject("my subject");
         message.setText("my text <img src='cid:myLogo'>", true);
         message.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
       }
     });
    

    The body of the message is one of the parts of the multipart message (note the second parameter set to true on the constructor of MimeMessageHelper that sets the message to a multipart message).

    The line message.addInline("myLogo"... adds the image as another part of the multipart message.

    You can use .setText to set the body (HTML content) of the email message.

    You can refer to other parts of the multipart email (your image) by using the tag cid. Note how the img src attribute is src='cid:myLogo'. cid is the content id of the image, sent as one of the parts of the multipart message.

    0 讨论(0)
提交回复
热议问题