日期:2014-05-17  浏览次数:20583 次

Velocity引擎渲染html格式邮件模版乱码问题

java 发送邮件,可以选择spring接口或javamail,网上关于这两种方式的代码有很多。


如果需要发送html格式的邮件,使用模版会比较方便。


BodyPart html = new MimeBodyPart();

html.setContent(content, "text/html; charset=UTF-8");


其中content利用VelocityEngineUtils的mergeTemplateIntoString方法渲染。


String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "mailTemplate.vm", "GBK", map);


注意velocityEngine一定要这样配置,否则会乱码。乱的一塌糊涂~~~~


<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">

<property name="velocityProperties"> ?

? ? ? ? ? ? ? <props>?

? ? ? ? ? ? ? ? ? <prop key="input.encoding">GBK</prop> ?

? ? ? ? ? ? ? ? ? <prop key="output.encoding">GBK</prop> ?

? ? ? ? ? ? ? ? ? <prop key="default.contentType">text/html; charset=GBK</prop> ?

? ? ? ? ? ? ? </props> ?

? ? ? ? </property>

</bean>


<