Oh, you use Java.
Please note that, in my opinion, you should always indicate an alternative to plain text in the HTML letter.
This code also allows you to create inline images (link to HTML using <img src="cid:foo">, but not all email clients support this.
MimeMessage mm = prepareMessage(from, to, subject, cc, bcc);
MimeMultipart mp = new MimeMultipart("alternative");
MimeBodyPart plain = new MimeBodyPart();
plain.setText(plainText);
mp.addBodyPart(plain);
MimeMultipart htmlmp = new MimeMultipart("related");
MimeBodyPart htmlbp = new MimeBodyPart();
htmlbp.setContent(htmlmp);
mp.addBodyPart(htmlbp);
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlText, "text/html");
htmlmp.addBodyPart(html);
for (EmailImage ei : template.getImages()) {
MimeBodyPart img = new MimeBodyPart();
img.setContentID(ei.getFilename());
img.setFileName(ei.getFilename());
ByteArrayDataSource bads = new ByteArrayDataSource(ei.getImageData(), ei.getMimeType());
img.setDataHandler(new DataHandler(bads));
htmlmp.addBodyPart(img);
}
mm.setContent(mp);
source
share