Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send UTF-8 encoded email body with 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("[email protected]");
    helper.setTo("[email protected]");

    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: [email protected]
To: [email protected]
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.

like image 711
jabal Avatar asked May 17 '15 16:05

jabal


People also ask

How do I encode a string using utf8?

In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array. where charsetName is the specific charset by which the String is encoded into an array of bytes.

How to send mail using java mail API?

Steps to send email using JavaMail APIGet the session object that stores all the information of host like host name, username, password etc. compose the message. send the message.


2 Answers

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.

like image 93
Woland Avatar answered Sep 18 '22 08:09

Woland


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("[email protected]");
     message.setTo("[email protected]");
     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"));
   }
 });
like image 29
Andrzej Skierski Avatar answered Sep 17 '22 08:09

Andrzej Skierski