Preparing a Simple Multipage / Alternative Email Using MimeMessageHelper (Spring Framework)

I would like to prepare a simple html email address with an alternative text version. I do not need any attachments or inline elements.

By default, if I use:

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

I get a mode MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED.

The body of my electronic content is as follows:

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 borders?

An ideal solution will be MimeMessageHelper.MULTIPART_MODE_ALTERNATIVE, but it is not available.

+3
source share
2 answers

There is also an easy way to do this:

MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
messageHelper.setFrom("your@mail.com");
messageHelper.setTo("target@mail.com");
messageHelper.setSubject("Spring mail test");
messageHelper.setText("Plain message", "<html><body><h2>html message</h2></body></html>");
javaMailSender.send(message);
+5
source

- html- , , spring MimeMessageHelper.MULTIPART_MODE_ALTERNATIVE. , MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, .

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

, BodyPart MimeMultipart, , BodyPart .

Pro spring 2.5 chapter13 - § HTML- ; APRESS ISBN-13 (pbk): 978-1-59059-921-1

+4

Source: https://habr.com/ru/post/1760870/


All Articles