How to send UTF-8 encoded email body using JavaMailSenderImpl?

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 install utf-8 whenever 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
.

Mostly the headers say UTF-8, but the outgoing bytes already contain a question mark where ő and should appear, which are two characters not found in Latin1. The JVM is file.encodingnot UTF-8, but I am looking for a way to keep it as it is and solve this problem only on the email side.

Thank!

Update

I previously successfully used the simple old method to send emails, and interestingly, this 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"); 

, - JavaMailSenderImpl.

+4
1

:

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

, Content-Transfer-Encoding: 8 , .

+1

All Articles