java freemark发送模板邮件

1、需要的jar包

   freemarker-2.3.15.jar

  mail.jar


2、模板文件夹

src/mailTemplete/


3、模版文件。如:

aaa.ftl


4、具体实现:

        @Resource
	private JavaMailSenderImpl mailSender;

	@Resource
	private FreeMarkerConfigurer freeMarkerConfigurer;

/**

     * 发送HTML模板邮件

     *

     * @param to收件人

     * @param subject邮件标题

     * @param map动态数据map

     * @param templateName模板名

     * @return

     */

    public boolean sendHtmlMail(String to, String subject, Map, ?> map,

            String templateName) {

        // 创建MimeMessage

        MimeMessage message = mailSender.createMimeMessage();

        try {

            // 创建helper类

            MimeMessageHelper helper = new MimeMessageHelper(message, true,

                    "utf-8");

            // 创建邮件接收者地址

            helper.setTo(to);

            // 创建邮件发送者地址

            helper.setFrom(mailSender.getUsername());

            // 设置标题

            helper.setSubject(subject);

            // 模板邮件内容

            String htmlText = "";

            // 通过指定模板名获取FreeMarker模板实例

            try {

                Template tpl = freeMarkerConfigurer.getConfiguration()

                        .getTemplate(templateName);

                // 解析模板并替换动态数据

                htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(

                        tpl, map);

            } catch (Exception e) {

                log.error("获取模板文件异常", e);

                e.printStackTrace();

            }



            // 设置邮件内容

            helper.setText(htmlText, true);

            // 发送邮件

            mailSender.send(message);

        } catch (Exception e) {

            log.error("邮件发送失败", e);

            return false;

        }

        return true;

    }
 

 

 

XML配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

	<!-- 邮件发送器 -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.live.com" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<!-- hotmail和gmail需要配置此项 -->
				<prop key="mail.smtp.starttls.enable">true</prop>
			</props>
		</property>

		<property name="username" value="[email protected]" />
		<property name="password" value="hitrader1314" />
	</bean>

	<!--配置邮件模板 -->
	<bean id="freeMarker"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/mailTemplete" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">15</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
			</props>
		</property>
	</bean>
</beans>



 

 

你可能感兴趣的:(freemarker,jml)