Chen Jian's Java Blog 

Sending html emails with images inside

by Chen Jian


Posted on 2019-05-26 15:04 in Java


If the images are external image links that can be accessed by http url, then nothing speical needs to be done.

If the images are external image files, then you have 2 choices.

Embed the images as base64 strings. . This is simple. However email clients such as gmail will refuse to render them

Using cid. This is the most robust way. An example can be found here . In addition, you should set the content type of the image body part as "image/png", otherwise the email receipients will see there are attachments

     
              MimeBodyPart imagePart = new MimeBodyPart();
                DataSource fds = new FileDataSource(imageFile);
                imagePart.setDataHandler(new DataHandler(fds));
                imagePart.setHeader("Content-ID", "<" + imageFile.getName() + ">");
                imagePart.setHeader("Content-Type", "image/png");



No one has commented yet.
该日志评论功能被禁用了。