通过Spring 3 JavaMail创建MIME格式的Freemarker模板的多部分消息 [英] Create multi-part message in MIME format Freemarker template via Spring 3 JavaMail

查看:138
本文介绍了通过Spring 3 JavaMail创建MIME格式的Freemarker模板的多部分消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为同一内容创建包含文本和HTML版本的电子邮件?

How do you create email message that contains text and HTML version for the same content?

当然,我想知道如何设置freemarker模板或将要发送的消息的标题。

Of course I would like to know how to setup the freemarker template or the header of the message that will be send.

当我查看收件箱中收到的MIME格式的消息多部分消息的源,每次都在这里,这是什么:

When I look on the source of message multi-part message in MIME format that I receive in inbox every once in while this is what is in there:

This is a multi-part message in MIME format.

------=_NextPart_000_B10D_01CBAAA8.F29DB300
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

...Text here...

------=_NextPart_000_B10D_01CBAAA8.F29DB300
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html><body> html code here ... </body></html>


推荐答案

如果发现任何不一致,请让我知道。我不得不从相当复杂的对象中提取出来,这就是为什么看起来像这样。

If you spot any inconsistencies please let me know. I had to extract this from pretty complex object so that's why this looks like it does.

//some important imports
import freemarker.template.Template;
import org.springframework.mail.javamail.*;
import org.springframework.context.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.mail.internet.MimeMessage;

private JavaMailSender mailSender;
private MessageSource messageSource;
private ExecutorService executor = Executors.newFixedThreadPool(50);

MimeMessagePreparator preparator = new MimeMessagePreparator() {
    public void prepare(MimeMessage mimeMessage) throws Exception {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

            message.setFrom(from);
            message.setTo(recipient);
            message.setSubject(subject);

            // Now the message body.
            Multipart mp = new MimeMultipart();

            BodyPart textPart = new MimeBodyPart();
            Template textTemplate = freemarkerConfig.getConfiguration().getTemplate(textEmailTemplate); // "/WEB-INF/emailText/*.ftl"
            final StringWriter textWriter = new StringWriter();
            textEmailTemplate.process(modelMap, textWriter);
            textPart.setText(textWriter.toString()); // sets type to "text/plain"


            BodyPart pixPart = new MimeBodyPart();
            Template pixTemplate = freemarkerConfig.getConfiguration().getTemplate(pixEmailTemplate); // "/WEB-INF/emailPix/*.ftl"
            final StringWriter pixWriter = new StringWriter();
            textEmailTemplate.process(modelMap, pixWriter);
            pixPart.setContent(pixWriter.toString(), "text/html");

            // Collect the Parts into the MultiPart
            mp.addBodyPart(textPart);
            mp.addBodyPart(pixPart);
            // Put the MultiPart into the Message
             message.setContent(mp);                  

     }
};

executor.submit(new SendMail(preparator));

class SendMail extends Thread {
    MimeMessagePreparator preparator;

    SendMail(MimeMessagePreparator preparator) {
        this.preparator = preparator;
    }

    public void run() {
        mailSender.send(preparator);
      }
}

这篇关于通过Spring 3 JavaMail创建MIME格式的Freemarker模板的多部分消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆