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.