问题来自主题。我正在使用Spring 3.0.3.RELEASE和velocity 1.6.4中的JavaMailSenderImpl从模板进行邮件准备。
当我从我的webapp收件人发送带有克罗地亚字符的电子邮件时,收到“?”在正常的克罗地亚人的立场。如果我为邮件调试调试模式, 从日志我可以看到内容类型设置为:
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
如何将其设置为:
Content-Type: text/html; charset=utf-8?
我正在开发此webapp时使用gmail进行邮件发送。
以下是我在spring的servlet xml conf文件中的设置:
<bean id="userAuthorizationManager" class="com.mypackage.manage.SimpleUserAuthorizationManagerImpl">
<property name="mailSender" ref="mailSender" />
<property name="velocityEngine" ref="velocityEngine" />
<property name="from" value="address" />
<property name="authorizationAddress" value="some text" />
<property name="subject" value="some text" />
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="user" />
<property name="password" value="pass" />
<property name="protocol" value="smtps" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.connectiontimeout">5000</prop>
<prop key="mail.smtp.sendpartial">true</prop>
<prop key="mail.smtp.userset">true</prop>
<prop key="mail.mime.charset">UTF-8</prop>
<prop key="mail.smtp.isSecure">true</prop>
<prop key="mail.smtp.requiresAuthentication">true</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
以下是来自邮件管理员的代码:
private void sendConfirmationEmail(final User user, final int requestId) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(user.getEmailAddress());
message.setFrom(from);
message.setSubject(subject);
Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
model.put("authorizationAddress", authorizationAddress);
model.put("requestId", requestId);
String text = VelocityEngineUtils
.mergeTemplateIntoString(
velocityEngine,
"com/mypackage/mail/registration-confirmation.vm",
model);
message.setText(text, true);
}
};
PendingMail pendingMail = new PendingMail(preparator);
pool.submit(pendingMail);
}
private class PendingMail implements Callable<Object> {
MimeMessagePreparator preparator;
protected PendingMail(MimeMessagePreparator preparator) {
this.preparator = preparator;
}
public Object call() {
mailSender.send(preparator);
return null;
}
}
你还有其他建议吗?
此致 Tiho
答案 0 :(得分:16)
答案 1 :(得分:2)
您可以在JavaMailSenderImpl spring bean配置中将其设置为属性mail.mime.charset
。请参阅此previous query回答的示例。
答案 2 :(得分:1)
2020年的答案,因为现在很少使用XML配置:
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.getJavaMailProperties().put("mail.mime.charset", "UTF-8");
答案 3 :(得分:0)
另一个选择可能是:
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setDefaultEncoding("UTF-8");