通过MimeMessageHelper以其他语言发送邮件时出现编码问题

时间:2018-06-26 10:45:17

标签: java spring-boot

Am以不同的语言(例如法语)发送邮件,通过MimeMessageHelper通过spring boot应用程序进行修饰。我使用html作为邮件模板。最终用户收到邮件时,编码不正确。

请检查屏幕截图。

这是我的代码

    private void send(String to, List<String> cc, String subject, String text, String mailFileName) {
    String fileName = "mails/Docs/angular-guide.pdf";
    try {
        MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true, "UTF-8");
        message.setFrom(from);
        message.setTo(to);
        if (cc != null && cc.size() > 0) {
            String[] array = new String[cc.size()];
            message.setCc(cc.toArray(array));
        }
        message.setSubject(subject);
        message.setText(text, true);

        if (mailFileName.equalsIgnoreCase("invitation-set-password.html")) {
            Resource resource = new ClassPathResource(fileName);
            File file = resource.getFile();         
            FileSystemResource fileSystem = new FileSystemResource(file);
            message.addAttachment("Rangular-guide.pdf", new InputStreamSource() {
                @Override
                public InputStream getInputStream () throws IOException {
                    return fileSystem.getInputStream();
                    }
                }, "application/pdf");
        }

        this.mailSender.send(mimeMessage);
    } catch (MessagingException e) {
        throw new UncheckedMessagingException(e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

邮件获得了法语的最终用户

联合国全国统一契约世界契约》,2003年正式定稿,适用于交通和法律原则,适用于全体雇员。 ,肯定的解决方案-负责的conconir-负责开发的conomique。

谢谢

1 个答案:

答案 0 :(得分:0)

您正在使用message.setText(text, true);设置消息的文本,暗示它是HTML消息。如果是纯文本而不是HTML,请尝试“ message.setText(text,false);”。如果是HTML,请确保在标头和?xml元素中正确设置了编码-是,请使用XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Demystifying Email Design</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
</html>

(有关HTML电子邮件模板here的更多信息)。

另外,当我在电子邮件中的字母编码方面遇到问题时,我将其发送到在Mozilla Thunderbird中配置的帐户,然后在新标签页中打开邮件,最后单击“更多”->“显示源”。这样一来,我就可以准确地看到发送到邮件客户端的电子邮件,并检查所有编码设置是否正确。

最后,如果以上方法均无济于事,则可以尝试将非拉丁字母映射到其unicode码,例如。 À表示\ u0192,à表示\ u0224,依此类推,here是完整列表。