7

I'm sending an email with the JavaMailSender with html in the body like this:

 String html = "<h2>RFC INVALIDOS en México:</h2>"+
                "<h4>Se adjunta el siguiente listado de RFC inválidos al día de la fecha.</h4>" +
                "<h3>Saludos!!!</h3>";

MimeMessageHelper helper = return new MimeMessageHelper(mimeMessage, true); // some helper
            helper.setSubject(message.getSubject());
            helper.setText(html, true);

Look at the vowels, (á, é, í) in "México", "inválidos" and "día"

and the mail is sended clipped, telling me is something more to see:

Mail body

Notice the part:

...

[Mensaje recortado] Ver todo el mensaje

But if I send it without quoting the vowels:

 String html = "<h2>RFC INVALIDOS en Mexico:</h2>"+
                "<h4>Se adjunta el siguiente listado de RFC invalidos al dia de la fecha.</h4>" +
                "<h3>Saludos!!!</h3>";

**look at the vowels **Look at the vowels, (a, e, i), in "mexico", "invalidos" and "dia"****

The mail is correctly and perfectly sent.

Correct mail Body

Any clues?

developer_hatch
  • 15,898
  • 3
  • 42
  • 75

5 Answers5

3

Finally with the indirect help of @Anish B. I found the solution:

There is a class MimeMessageHelper and that class has several constructors:

public MimeMessageHelper(MimeMessage mimeMessage)

public MimeMessageHelper(MimeMessage mimeMessage, String encoding)

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart)

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart, String encoding)

I was using:

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart)

And I changed it for:

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart, String encoding)

And it looks like:

protected MimeMessageHelper createMimeMsg(MimeMessage mimeMessage) throws MessagingException {
        return new MimeMessageHelper(mimeMessage, true, "UTF-8");
    }

It looks like the default encoding is not "UTF-8", the defaul encoding is null for that helper. And even when I set the html as "UTF-8" it doesn't recognize it. I had to put it as a part of the subject encoding for mime.

So finally the problem is solved by giving the encoding correctly.

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
2

Your email editor 'folds' the mail body when the 'body height' is 'beyond certain height' that the software watches for.

Now, your overall 'mail body height' is the sum of each line in your mail body. Now each letter has a height, and that is varying based on whether you are using letters with 'accents' on it, the 'height' of the letter with and without accent character need not be the same.

In this particular example, the former did not attract 'folding', while the latter did in the email software that you used.

Assuming you are viewing these three lines in a browser, you can research on 'line heights' in a browser 'developer tools' and finally determine that individual line heights are more when you used accented characters.

I bet, reduce the font size and you should not get that '...' (ellipsis). Below is a much more explosive discussing with thorough history and spec references and what not on technically dissecting a font.

When setting a font-size in CSS, what is the real height of the letters?

Siva
  • 598
  • 3
  • 11
  • The problem was about the encoding , in this case UTF-8, if you post a full answer with a mail example without being clipped I will gladly accept and give the bounty to you – developer_hatch Jul 06 '19 at 21:30
2

What I tried:

MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("subject", "UTF-8"); // add encoding to support different languages...
message.setSentDate(new java.util.Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
String html = "<h2>RFC INVALIDOS en México:</h2>"+
                "<h4>Se adjunta el siguiente listado de RFC inválidos al día de la fecha.</h4>" +
                "<h3>Saludos!!!</h3>";
messageBodyPart.setContent(html, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// send message
Transport.send(message);

Screenshot of the mail (all vowels and accents are present):

enter image description here

Hope this helps you :)

The reason for clipping message by Gmail (From MailChimp Article on Gmail Clipping)

Gmail clips emails that have a message size larger than 102 KB, and hides the full content behind a View entire message link.

So, if you want to avoid clipping of messages, then you have to keep the message size less than 102 KB.

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
Anish B.
  • 9,111
  • 3
  • 21
  • 41
2

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

This is found here: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/mail/javamail/MimeMessageHelper.html

It is implemented in their example as an abstract object definition:

mailSender.send(new MimeMessagePreparator() {
   public void prepare(MimeMessage mimeMessage) throws MessagingException {
      MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // Here is where it seems the encoding can be set, through this helper class. 
//...

I've also just found something cool now looking at the api. It may even be easier for you, if, you can make your signature a small kb image resource, and send it that way. This makes you not have to worry about UTF-8 in this scenario.

message.addInline("signature.png", new ClassPathResource("img/signature.png"));

Anyway, hope this helps. Let me know if I can try to explain it better for you. (UTF-8)

Joe
  • 1,316
  • 9
  • 17
1

Point #1: Previous StackOverflow answer regarding UTF-8 & GMail

utf 8 charset doesn't work with javax mail

Point #2: You might want to add this line, it works for HTML pages, and it might work for your problem:

<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />