通过 Spring 3 JavaMail 创建 MIME 格式 Freemarker 模板的多部分消息

发布于 2024-10-10 16:34:49 字数 526 浏览 2 评论 0原文

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

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

当我查看收件箱中每次收到的 MIME 格式的多部分消息的来源时,其中的内容如下:

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>

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

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-10-17 16:34:49

如果您发现任何不一致之处,请告诉我。我必须从相当复杂的对象中提取它,这就是为什么它看起来像这样。

//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);
      }
}

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);
      }
}
缺⑴份安定 2024-10-17 16:34:49

使用 Spring 时,您可以执行以下操作:

  String plainText = "MyPleinText";
  String htmlText = "<html><body><h1>MyHTML</h1></body></html>";
  MimeMessage message = this.mailSender.createMimeMessage();
  MimeMessageHelper helper = new MimeMessageHelper(message, true, MAIL_ENCODING);
  helper.setText(plainText,htmlText);

它会完成这项工作。
不涉及 Freemarker 的东西。

MailSender 可以是:

  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${smtp.host}" />
    <property name="port" value="${smtp.port}" />
    <property name="defaultEncoding" value="UTF-8" />
  </bean>

When using Spring you can do:

  String plainText = "MyPleinText";
  String htmlText = "<html><body><h1>MyHTML</h1></body></html>";
  MimeMessage message = this.mailSender.createMimeMessage();
  MimeMessageHelper helper = new MimeMessageHelper(message, true, MAIL_ENCODING);
  helper.setText(plainText,htmlText);

And it will do the job.
There is no Freemarker stuff involved.

The MailSender can be:

  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${smtp.host}" />
    <property name="port" value="${smtp.port}" />
    <property name="defaultEncoding" value="UTF-8" />
  </bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文