MimeMessageHelper附件文件名编码

MimeMessageHelper附件文件名编码

本文介绍了Spring MimeMessageHelper附件文件名编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Boot应用程序中使用MimeMessageHelper发送邮件.

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?

在构造MimeMessageHelper时将编码设置为UTF-8似乎没有帮助.在Gmail中,生成的附件显示为

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

推荐答案

我已经解决了以下几行的问题:

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)
  • System.setProperty("mail.mime.splitlongparameters", "false")
  • MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8")
  • MimeUtility.encodeWord(attachmentFilename)

这是示例代码,

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
);

这篇关于Spring MimeMessageHelper附件文件名编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:17