spring3邮件模板velocityEngine UTF-8乱码

乱码问题每个人都遇到过,工作这么久也经常会在不同的情况下遇到这个问题,前两天的spring邮件里使用velocity模板的时候又乱码了,还好解决了

乱码问题每个人都遇到过,工作这么久也经常会在不同的情况下遇到这个问题,前两天的spring邮件里使用velocity模板的时候又乱码了,还好解决了
首先修改spring bean的配置
[ccn lang="xml" tab_size="4" theme="blackboard" width="550" ]


resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
input.encoding=UTF-8
output.encoding=UTF-8
default.contentType=text/html;charset=UTF-8

[/ccn]
然后在使用的时候
[ccn lang="java" tab_size="4" theme="blackboard" width="550" ]
public void sendMail_yqlsmail(PUser user,VYqlsmailBody yqlsmailBody,VJssqmail jssqmail){

yqlsmailBody.setUserName(user.getChn_name());
yqlsmailBody.setUserEmail(user.getEmail());
yqlsmailBody.setCellPhone(user.getMobile_phone());
yqlsmailBody.setTel(user.getTelephone());
yqlsmailBody.setTeachers(jssqmail.getMailto());

// 邮件准备
MimeMessagePreparator preparator = preparator_yqlsmail(user, yqlsmailBody,jssqmail);
// 发送邮件
mailService.sendSampleMail(preparator);
}

private MimeMessagePreparator preparator_yqlsmail(final PUser user,
final VYqlsmailBody yqlsqmailBody, final VJssqmail jssqmail) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
true, "UTF-8");
message.setTo(jssqmail.getMailtoArray());
//message.setCc(jssqmail.getMailcc());
message.setFrom("bj_bug@staff.xxxx.org"); // could be
message.setSubject("邀请函 (" + yqlsqmailBody.getJiangzuoxuexiao() + ")"); // parameterized...
Map model = new HashMap();
model.put("yqlsmailvo", yqlsqmailBody);
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine,
"org/xxxx/sc/bj/mail/template/jiangshiyaoqing.vm",
"UTF-8", model);
message.setText(text, true);
}
};
return preparator;
}
[/ccn]