I have the following Java
code in my Spring
REST Web App:
Code:
@Service("Mailer")
public class Mailer {
private final JavaMailSender mailSender;
private void send(final Email email){
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(address);
helper.setTo(email.to());
helper.setSubject(email.subject());
helper.setText(email.content(), true); //is this correct?
mailSender.send(mimeMessage);
}
}
MimeMessageHelper setText():
public void setText(String text, boolean html) throws MessagingException {
Assert.notNull(text, "Text must not be null");
MimePart partToUse;
if (isMultipart()) {
partToUse = getMainPart();
}
else {
partToUse = this.mimeMessage;
}
if (html) {
setHtmlTextToMimePart(partToUse, text);
}
else {
setPlainTextToMimePart(partToUse, text);
}
}
My code sends an email as expected but when I receive it - it is in pain text .
Ie the HTML
is not rendered in the email body and the email starts with:
<!DOCTYPE html>
<html lang="en">
<head>
...
How can I ensure that the email is sent and rendered as HTML
not plain text?
Try setting the MimeMessage content directly like this.
mimeMessage.setContent(email.content(), "text/html");
mimeMessage.saveChanges();
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.