首页 > 解决方案 > JavaMail 更新后的附件文件名字符集问题

问题描述

从 JavaMail 1.4.5 更新到 1.6.2 后,我们遇到了附件文件名字符集的问题。这些问题至少部分与 相关encodeparameters,因为我们希望false在新版本中将其设置为,以避免接收者的附件出现问题。这都是在 Windows 环境中执行的。

使用 JavaMail 1.4.5 我们有这样的经历:

Content-Type: application/octet-stream; name="Frammøtebekreftelse.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Frammøtebekreftelse.pdf"

使用 JavaMail 1.6.2 我们有这样的经历:

Content-Type: application/octet-stream; 
    name*=windows-1252''Framm%F8tebekreftelse.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; 
    filename*=windows-1252''Framm%F8tebekreftelse.pdf

我认为这很好,但是接收者对name*-format 有问题,这导致我们使用encodeparameters=false.

使用 JavaMail 1.6.2encodeparameters=false我们有这样的经历:

Content-Type: application/octet-stream; name="Frammøtebekreftelse.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Frammøtebekreftelse.pdf"

正如你所看到的,这几乎是我们不想要的,只是现在“Ø”字符被破坏了。其余代码相同。我已经研究了设置字符编码的各种方法,但似乎没有一个解决结果的这个特定部分。

我也尝试过使用 JakartaMail 1.6.5 的最后一个版本,结果相同。

我需要设置什么代码或什么 JVM 选项才能做到这一点?

这个代码很普通。下面是一个 MCVE,应该可以证明这个问题:

import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mcve {
    public static void main(String[] args) throws AddressException, MessagingException, IOException {
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", "localhost");
        props.setProperty("mail.mime.encodeparameters", "false");

        Session session = Session.getInstance(props, null);
        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress("email@example.com"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("email@example.com", false));
        message.setSubject("Frammøtebekreftelse");

        MimeMultipart multipart = new MimeMultipart();

        MimeBodyPart body = new MimeBodyPart();
        body.setContent("Hello world", "text/plain");
        multipart.addBodyPart(body);

        MimeBodyPart attachment = new MimeBodyPart();
        attachment.attachFile("Frammøtebekreftelse.pdf");
        multipart.addBodyPart(attachment);

        message.setContent(multipart);

        Transport.send(message);
    }
}

标签: javaemailencodingcharacter-encodingjakarta-mail

解决方案


为了获得 JavaMail 1.6.2 中标题的旧外观,我需要添加以下两个系统属性:

props.setProperty("mail.mime.encodeparameters", "false");
props.setProperty("mail.mime.allowutf8", "false");

补丁说明中mail.mime.allowutf8的解释:

mail.mime.allowutf8:

  If set to "true", UTF-8 strings are allowed in message headers,
  e.g., in addresses.  This should only be set if the mail server also
  supports UTF-8.

不幸的是,我并不完全相信这不会弄乱其他一些功能。从我的基本检查来看,差异至少在于LineOutputStream.

1.4.5:

public void writeln(String s) throws IOException {
    byte[] bytes = ASCIIUtility.getBytes(s);
    out.write(bytes);
    out.write(newline);
}

1.6.2:

public void writeln(String s) throws IOException {
    byte[] bytes;
    if (allowutf8)
        bytes = s.getBytes(StandardCharsets.UTF_8);
    else
        bytes = ASCIIUtility.getBytes(s);
    out.write(bytes);
    out.write(newline);
}

推荐阅读