Spring 在生产环境中部署应用程序时velocity模板中的编码问题
我使用velocity模板向我的用户发送电子邮件。这些电子邮件包含“s”、“ñ”或“s”等元素。当我在本地发送电子邮件时,一切都很好,没有遇到任何问题 然而,如果应用程序处于部署环境中,则在使用上述字符时,电子邮件显示错误 这是我发送电子邮件的方法:Spring 在生产环境中部署应用程序时velocity模板中的编码问题,spring,templates,encoding,velocity,Spring,Templates,Encoding,Velocity,我使用velocity模板向我的用户发送电子邮件。这些电子邮件包含“s”、“ñ”或“s”等元素。当我在本地发送电子邮件时,一切都很好,没有遇到任何问题 然而,如果应用程序处于部署环境中,则在使用上述字符时,电子邮件显示错误 这是我发送电子邮件的方法: MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) th
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setTo(to);
message.setFrom(from);
message.setReplyTo(from);
message.setSubject(subject);
String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateURI, "UTF-8", model);
message.setText(body, true);
}
};
try{
this.mailSender.send(preparator);
}catch(MailException e){
logger.error("Mail exception sending the email to "+ to + "\n Exception: " + e.getMessage());
}
}
这是一个模板示例:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>[Manager]</title>
<style>
body{font-family:"Courier New", Courier, monospace};
.message{font-size:0.8em};
</style>
</head>
<body>
<p> Dear ${user.firstName},</p>
<p> ${message} </p>
<p> Best regards, </p>
<p> Manager <br/>
Manager system app
</p>
<p class="message">This message was sent by the admin. Do not answer this email.<br>
If you have any issue, do not hesitate to contact us through the support section in your user panel</p>
</body>
</html>
[经理]
正文{字体系列:“Courier New”,Courier,monospace};
.message{字体大小:0.8em};
亲爱的${user.firstName}
${message}
致以最良好的祝愿
经理
管理系统应用程序
这就是豆子:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
contentType=text/html; charset=utf-8
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
resource.loader=class
contentType=text/html;字符集=utf-8
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
我们在生产服务器中使用tomcat7。它是否需要任何特定的配置,或者是应用程序配置中的问题?您可以尝试在velocity属性中设置其他两个属性:
input.encoding = UTF8
output.encoding = UTF8