8

I am sending an email this way:

@Test
public void testEmailCharacterSet() throws MessagingException, UnsupportedEncodingException {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setDefaultEncoding("utf-8");
    mailSender.setHost("*****");
    mailSender.setUsername("*****");
    mailSender.setPassword("*****");

    Properties properties = new Properties();
    properties.setProperty("mail.mime.charset", "utf-8");

    mailSender.setJavaMailProperties(properties);

    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
    mimeMessage.setContent("Árvíztűrő tükörfúrógép 3", "text/html"); 
    helper.setFrom("noreply@foobar.com");
    helper.setTo("foobar@gmail.com");

    mailSender.send(mimeMessage);
}

As you can see I am setting utf-8 everywhere when I can. My problem is that the outgoing raw bytes are still in Latin1, at least this is what I see in Wireshark:

Date: Sun, 17 May 2015 18:16:21 +0200 (CEST)
From: noreply@foobar.com
To: foobar@gmail.com
Message-ID: <13648335.0.1431879381653.JavaMail.foo@foo-dell>
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

=C1rv=EDzt?r? t=FCk=F6rf=FAr=F3g=E9p 3
.

So basically the headers are saying UTF-8, but the outgoing bytes already contain question mark where ő and ű should appear, which are two characters that are missing from Latin1. The JVM's file.encoding is not UTF-8, but I am looking for a way to keep that as it is and solve this problem only in the emailing side.

Thanks!

Update

I have previously successfully used the plain old method to send email, and it's interesting that that still works:

Message mimeMessage = new MimeMessage(session);
mimeMessage.setContent("Árvíztűrő tükörfúrógép 7 oldschool", "text/html; charset=utf-8"); 

So it's clearly something specific to the JavaMailSenderImpl only.

jabal
  • 11,987
  • 12
  • 51
  • 99
  • what if, in your java source code, you replace `ű` with `\u0171` ? what would be the result then? – ZhongYu May 17 '15 at 17:22
  • 1
    That also appears as a "?" on the wire. I think it's around the `quoted-printable` transfer encoding – jabal May 17 '15 at 17:29
  • not likely, quoted-printable only care about bytes in the input. the bug is apparently due to `String->byte[]` conversion and a `char` is unrecognized in that conversion. – ZhongYu May 17 '15 at 17:31
  • what if you do `mimeMessage.setContent( string.getBytes("UTF-8") )` – ZhongYu May 17 '15 at 17:32
  • That throws exception, about byte[] not being a valid input. – jabal May 17 '15 at 17:35
  • oh man, I'm not really familiar with this topic. If you do set JVM encoding to UTF-8, it will work, right? Apprently, some code is converting the String to byte[] using the JVM default encoding, and we need to change that. – ZhongYu May 17 '15 at 17:37
  • No, setting -Dfile.encoding to utf-8 does not help – jabal May 17 '15 at 18:39
  • so it's fixed to Latin-1? that sucks. maybe you need to debug and step into the code. – ZhongYu May 17 '15 at 18:57
  • If you're using an IDE, such as Eclipse, make sure that you've set the encoding of your source code to UTF-8. If you're not using an IDE, you have to tell `javac` your encoding: `javac -encoding UTF-8` – Alastair McCormack May 18 '15 at 20:36
  • Thanks, but I have my sources in UTF-8, when I re-echo to console the string, it's fine, and in runtime this comes from database anyways. But special chars are fine there too. – jabal May 21 '15 at 18:32

3 Answers3

5

If you add this lines:

properties.setProperty("mail.smtp.allow8bitmime", "true");
properties.setProperty("mail.smtps.allow8bitmime", "true");

to your code, message will have Content-Transfer-Encoding: header set to 8bit and mail will be readable.

Woland
  • 81
  • 3
  • 6
5

This works for me - link

mailSender.send(new MimeMessagePreparator() {
   public void prepare(MimeMessage mimeMessage) throws MessagingException {
     MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
     message.setFrom("me@mail.com");
     message.setTo("you@mail.com");
     message.setSubject("my subject");
     message.setText("my text <img src='cid:myLogo'>", true);
     message.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
     message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));
   }
 });
1

In your application.properties files , Add these lines :

  • spring.mail.properties.mail.smtp.allow8bitmime =true
  • spring.mail.properties.mail.smtps.allow8bitmime =true