`

java freemark发送模板邮件

    博客分类:
  • java
阅读更多

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="support@hitrader.com" />
		<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>



 

 

分享到:
评论

相关推荐

    Java html转word 使用FreeMarker

    FreeMarker是一个开源的Java库,主要用于生成动态生成的Web页面或其他类型的文本输出,如电子邮件、XML文档等。它的核心功能是解析模板文件,并结合数据模型生成目标输出。在HTML到Word的转换过程中,FreeMarker能够...

    Java协同办公OA系统源码

    6邮件管理:员工内部邮件发送、转发,外部邮件发送等,右键支持上传附件。 7任务管理:新增任务、任务查看、任务状态修改等。 8日程管理:日程新建、查看、日程按照日、周、月显示在日历表等。 9计划管理:...

    Java基于springboot办公OA管理系统源码.7z

    前台框架是springboot+freemark+jpa+mybatis+mysql实现的OA自动化办公系统, 后台框架是springboot,持久层用了jpa和mybaits,模板用的是freemark, 开发环境:idea +jdk1.8+mysql...6邮件管理:员工内部邮件发送、等等

    freemark笔记、语法、讲解大全

    - 使用 FreeMarker 生成个性化邮件内容,比如根据用户的购买记录发送定制化的促销邮件。 - 示例:根据用户购买历史,生成个性化的推荐商品列表。 3. **报表生成**: - 使用 FreeMarker 生成复杂的数据报表,如...

    MF00807-Java协同办公OA管理源码.zip

    框架:springboot + freemark + jpa +mybatis + mysql 环境:jdk8 + mysql5.7 + IntelliJ IDEA + maven + nginx + 宝塔面板 这是一套OA办公系统,使用Maven作项目管理,基于springboot框架开发的项目, mysql作为...

    freemarker中文API手册.pdf

    生成的文本可以保存、发送邮件或直接返回给Web浏览器。 2. **强大的模板语言**: - 模板语言支持包括`include`、`if/elseif/else`、循环结构在内的常见指令。 - 可在模板中创建和修改变量,使用复杂表达式设定值...

    SpringBoot集成Freemarker+FlyingSaucer实现pdf在线预览.pdf

    Freemarker是Java中的一款模板引擎,可以根据模板和参数生成输出结果。在本方案中,Freemarker用于生成HTML模板,并将其转换为PDF文档。Freemarker的模板语法简单易懂,且支持各种数据类型和逻辑运算符,使其成为...

    ant + freemarker + excel解析

    4. 最后,Ant可以负责将生成的文件输出到指定位置,或者进行进一步的处理,比如发送电子邮件或上传到服务器。 总结来说,Ant提供构建自动化,FreeMarker用于模板渲染,而Apache POI则用于处理Excel文件。通过它们的...

    基于springboot OA自动化办公系统源码

    前台框架是springboot+freemark+jpa+mybatis+mysql实现的OA自动化办公系统,后台框架是springboot,持久层用了jpa和mybaits,模板用的是freemark,系统整体比较完整。系统功能模块有: 1系统管理:数据字典(数据...

Global site tag (gtag.js) - Google Analytics