java 使用 MimeMessageHelper (Spring Framework) 准备简单的多部分/替代电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3536826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:17:24  来源:igfitidea点击:

Preparing simple multipart/alternative email using MimeMessageHelper (Spring Framework)

javaspringemailmimejavamail

提问by roxel

I'd like to prepare simple html email with alternative plain-text version. I don't need any attachments or inline elements.

我想准备带有替代纯文本版本的简单 html 电子邮件。我不需要任何附件或内联元素。

By default if I use:

默认情况下,如果我使用:

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");

I get MimeMessageHelper.MULTIPART_MODE_MIXED_RELATEDmode.

我得到MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED模式。

My email content body looks like this:

我的电子邮件内容正文如下所示:

Content-Type: multipart/mixed; 
    boundary="----=_Part_8_21489995.1282317482209"

------=_Part_8_21489996.1282317482209
Content-Type: multipart/related; 
    boundary="----=_Part_9_21489996.1282317482209"

------=_Part_9_21489996.1282317482209
Content-Type: multipart/alternative; 
    boundary="----=_Part_10_2458205.1282317482209"

------=_Part_10_2458205.1282317482209
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Simple newsletter.

------=_Part_10_2458205.1282317482209
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
    <head>
        <title>Simple newsletter</title>
    <head>
    <body>
        <p>Simple newsletter.</p>
    </body>
<html>

------=_Part_10_2458205.1282317482209--

------=_Part_9_21489996.1282317482209--

------=_Part_8_21489995.1282317482209--

What can I do to get rid of mixed and related boundries?

我能做些什么来摆脱混合和相关的边界?

Ideal solution would be MimeMessageHelper.MULTIPART_MODE_ALTERNATIVEmode, but it's not available.

理想的解决方案是MimeMessageHelper.MULTIPART_MODE_ALTERNATIVE模式,但它不可用。

回答by redochka

Because of a spam checker issue, I was looking to send html email with alternative but it seems that spring just don't provide a simple MimeMessageHelper.MULTIPART_MODE_ALTERNATIVE. However using the MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED get my emails reach their destinations.

由于垃圾邮件检查器问题,我希望发送带有替代方法的 html 电子邮件,但似乎 spring 只是不提供简单的 MimeMessageHelper.MULTIPART_MODE_ALTERNATIVE。但是使用 MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED 让我的电子邮件到达目的地。

If you still want to get rid of mixed and related boundaries, you need more control over how a MIME message is assembled, you can create an implementation of the MimeMessagePreparator interface and pass this to the JavaMailSender.send() method in place of the MimeMessage.

如果您仍然想摆脱混合和相关边界,您需要更多地控制 MIME 消息的组装方式,您可以创建 MimeMessagePreparator 接口的实现并将其传递给 JavaMailSender.send() 方法代替 MimeMessage .

  sender.send(new MessagePreparator());
private class MessagePreparator implements MimeMessagePreparator {
public void prepare(MimeMessage msg) throws Exception {
// set header details
msg.addFrom(InternetAddress.parse(from));
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
msg.setSubject(subject);

// create wrapper multipart/alternative part
MimeMultipart ma = new MimeMultipart("alternative");
msg.setContent(ma);
// create the plain text
BodyPart plainText = new MimeBodyPart();
plainText.setText("This is the plain text version of the mail.");
ma.addBodyPart(plainText);
// create the html part
BodyPart html = new MimeBodyPart();
html.setContent(
"<html><head></head><body>
<h1>This is the HTML version of the mail."
+ "</h1></body></html>", "text/html");
ma.addBodyPart(html);
}
}
}

The order in which you add BodyPart instances to the MimeMultipart is important, and you should add the BodyPart with the most preferable message format last.

将 BodyPart 实例添加到 MimeMultipart 的顺序很重要,您应该最后添加具有最可取的消息格式的 BodyPart。

This is an excerpt taken from Pro Spring 2.5 chapter13 - § Sending an HTML Message with a Plain Text Alternative ; APRESS ISBN-13 (pbk): 978-1-59059-921-1

这是摘自 Pro Spring 2.5 第 13 章 - § 发送带有纯文本替代的 HTML 消息;APRESS ISBN-13 (pbk): 978-1-59059-921-1

回答by Jakub Kubrynski

There is also simper way to do that:

还有一种更简单的方法可以做到这一点:

MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
messageHelper.setFrom("[email protected]");
messageHelper.setTo("[email protected]");
messageHelper.setSubject("Spring mail test");
messageHelper.setText("Plain message", "<html><body><h2>html message</h2></body></html>");
javaMailSender.send(message);