我正在尝试使用JavaMailSender发送包含html内容的邮件,而不是在电子邮件中获取呈现的html,而不是在邮件中获取html代码本身,即
发送电子邮件的方法
public void sendMimeMessage(String from, String to, String subject, String messageBody, String... cc) {
MimeMessage message = mailSender.createMimeMessage()
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from)
helper.setSentDate(new Date())
helper.setSubject(subject)
helper.setText(messageBody, true)
helper.setTo(to)
helper.setCc(cc)
mailSender.send(message)
log.debug("Email successfully sent to <${to}> with cc <${cc}> and with subject <${subject}> and Email body: ${messageBody}")
} catch (Exception exception) {
exception.printStackTrace()
log.error("Email to <${to}> with subject <${subject}> could not be sent due to: ", exception)
}
}
任何帮助,将不胜感激。
最佳答案
This可能会帮助您。
您可以使用Apache velicity,也可以看到我的Answer。
样例代码:
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");
MimeMessage message = sender.createMimeMessage();
// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("test@host.com");
// use the true flag to indicate the text included is HTML
helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true);
// let's include the infamous windows Sample file (this time copied to c:/)
FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);
sender.send(message);
关于java - 邮件中HTML数据内容不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36473612/