in commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/HtmlEmail.java [193:275]
private void build() throws MessagingException, EmailException {
final MimeMultipart rootContainer = getContainer();
MimeMultipart bodyEmbedsContainer = rootContainer;
MimeMultipart bodyContainer = rootContainer;
MimeBodyPart msgHtml = null;
MimeBodyPart msgText = null;
rootContainer.setSubType("mixed");
// determine how to form multiparts of email
if (EmailUtils.isNotEmpty(html) && !EmailUtils.isEmpty(inlineEmbeds)) {
// If HTML body and embeds are used, create a related container and add it to the root container
bodyEmbedsContainer = new MimeMultipart("related");
bodyContainer = bodyEmbedsContainer;
addPart(bodyEmbedsContainer, 0);
// If TEXT body was specified, create a alternative container and add it to the embeds container
if (EmailUtils.isNotEmpty(text)) {
bodyContainer = new MimeMultipart("alternative");
final BodyPart bodyPart = createBodyPart();
try {
bodyPart.setContent(bodyContainer);
bodyEmbedsContainer.addBodyPart(bodyPart, 0);
} catch (final MessagingException e) {
throw new EmailException(e);
}
}
} else if (EmailUtils.isNotEmpty(text) && EmailUtils.isNotEmpty(html)) {
// EMAIL-142: if we have both an HTML and TEXT body, but no attachments or
// inline images, the root container should have mimetype
// "multipart/alternative".
// reference: https://tools.ietf.org/html/rfc2046#section-5.1.4
if (!EmailUtils.isEmpty(inlineEmbeds) || isBoolHasAttachments()) {
// If both HTML and TEXT bodies are provided, create an alternative
// container and add it to the root container
bodyContainer = new MimeMultipart("alternative");
this.addPart(bodyContainer, 0);
} else {
// no attachments or embedded images present, change the mimetype
// of the root container (= body container)
rootContainer.setSubType("alternative");
}
}
if (EmailUtils.isNotEmpty(html)) {
msgHtml = new MimeBodyPart();
bodyContainer.addBodyPart(msgHtml, 0);
// EMAIL-104: call explicitly setText to use default mime charset
// (property "mail.mime.charset") in case none has been set
msgHtml.setText(html, getCharsetName(), EmailConstants.TEXT_SUBTYPE_HTML);
// EMAIL-147: work-around for buggy JavaMail implementations;
// in case setText(...) does not set the correct content type,
// use the setContent() method instead.
final String contentType = msgHtml.getContentType();
if (contentType == null || !contentType.equals(EmailConstants.TEXT_HTML)) {
// apply default charset if one has been set
if (EmailUtils.isNotEmpty(getCharsetName())) {
msgHtml.setContent(html, EmailConstants.TEXT_HTML + "; charset=" + getCharsetName());
} else {
// unfortunately, MimeUtility.getDefaultMIMECharset() is package private
// and thus cannot be used to set the default system charset in case
// no charset has been provided by the user
msgHtml.setContent(html, EmailConstants.TEXT_HTML);
}
}
for (final InlineImage image : inlineEmbeds.values()) {
bodyEmbedsContainer.addBodyPart(image.getMimeBodyPart());
}
}
if (EmailUtils.isNotEmpty(text)) {
msgText = new MimeBodyPart();
bodyContainer.addBodyPart(msgText, 0);
// EMAIL-104: call explicitly setText to use default mime charset
// (property "mail.mime.charset") in case none has been set
msgText.setText(text, getCharsetName());
}
}