Unicode字符和Spring JavaMailSenderImpl,在Linux下没有Unicode字符! [英] Unicode chars and Spring JavaMailSenderImpl, no unicode chars under Linux!

查看:122
本文介绍了Unicode字符和Spring JavaMailSenderImpl,在Linux下没有Unicode字符!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring和JavaMailSenderImpl,这是一个著名的spring类,用于发送电子邮件.我的电子邮件中包含许多Unicode字符,例如èéàò或最引人注意的€符号.在Windows上运行时,我的班级工作正常.发送的电子邮件包含所有字符(纯文本,无html).如果将我的应用程序安装在Linux虚拟服务器上,我会得到所有?而不是特殊字符.是Spring,Java配置还是其他?

I'm using Spring and JavaMailSenderImpl, a famous spring class to send emails. My emails contain a lot of unicode chars like èéàò or most notably the dreaded € symbol. My classes work fine when the run on windows. The emails sent are with all the chars (plain text, no html). If I install my app on a Linux virtual server, I'll get all ? instead of the special chars. Is it Spring, Java configuration or something else?

更新

基本上,架构是这样的:有一个Spring Web Application,我使用spring JavaMailSenderImpl完成工作.这是servlet上下文中的配置:

Basically the architecture is this: there is a Spring Web Application and I use spring JavaMailSenderImpl to get the work done. This is the configuration in servlet-context:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${email.server}" />
    <property name="username" value="${email.server_user}"></property>
    <property name="password" value="${email.server_pass}"></property>
</bean>

我在Windows和Linux上使用同一主机发送邮件(这与应用程序运行所在的计算机不同...这只是SMTP上的标准邮件服务提供商).

I'm using the same host on windows and linux to send mail (that is not the same machine where the application runs on... It is just a standard mail service provider over SMTP).

我用来发送电子邮件的代码很简单:

The code I use to send the email is simply:

SimpleMailMessage msg = new SimpleMailMessage();
            msg.setTo(adminEmail);
            msg.setFrom(adminEmail);
            msg.setSubject(subject);
            msg.setText(message);
            mailSender.send(msg);

甚至设置:

System.setProperty("mail.mime.charset", "utf8");

在应用程序启动时

不能解决问题.实际上,在我获得之前?而不是€,现在我得到......

at application startup doesn't solve the situation. In fact, before I was getting ? instead of €, now I get �...

推荐答案

我正在发布解决方案.

首先确保源代码的编码(如果有内联文本).如果选择项目属性,则在eclipse中是第一个屏幕.警告:如果以后更改,它将使您的文本乱码.

First make sure about the encoding of the source code (if there is inline text). In eclipse is the first screen if you choose project properties. Warning: if you change it later it will garble your text.

第二最好使用MimeMailMessage,以便您可以指定编码,如下所示:

Second It is better to use MimeMailMessage so that you can specify the encoding, like this:

MimeMessage msg = mailSender.createMimeMessage();

        msg.addRecipient(RecipientType.TO, new InternetAddress(adminEmail));
        msg.addFrom(new InternetAddress[] { new InternetAddress(adminEmail) });

        msg.setSubject(subject, "UTF-8");
        msg.setText(message, "UTF-8");  
        mailSender.send(msg);

第三,请确保通过Java命令或类似这样的代码将系统属性mail.mime.charset设置为UTF-8:

Third make sure the system property mail.mime.charset is set to UTF-8, either from Java command or by code like this:

System.setProperty("mail.mime.charset", "utf8");

感谢所有帮助我解决问题的人.

这篇关于Unicode字符和Spring JavaMailSenderImpl,在Linux下没有Unicode字符!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆