Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MimeMessageHelper attachment filename encoding

I am sending mail with MimeMessageHelper in my Spring Boot application.

How can I tell it to encode the filename, which contains the letter à, so that it would display correctly?

Setting the encoding to UTF-8 when constructing MimeMessageHelper does not seem to help. In Gmail, the resulting attachment is displayed as

=?UTF-8?Q?ex-comp_s.=C3=A0_r.l.?= =?UTF-8?Q?-201\";     filename*1=\"7-07-12_=E2=80=95_2017-07-18
like image 512
yglodt Avatar asked Dec 24 '22 16:12

yglodt


1 Answers

I've solved the the problem with these lines:

  • System.setProperty("mail.mime.splitlongparameters", "false")
  • MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8")
  • MimeUtility.encodeWord(attachmentFilename)

Here is the example code,

System.setProperty("mail.mime.splitlongparameters", "false");
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

// Your email content
helper.setFrom("...");
helper.setTo("...");
helper.setSubject("...");
helper.setText("...");

helper.addAttachment(
    MimeUtility.encodeWord(attachmentFilename),
    attachmentContent
);
like image 58
wizawu Avatar answered Jan 10 '23 20:01

wizawu