在上一篇Spring电子邮件教程,硬编码的所有电子邮件属性和消息的方法体中的内容,这是不实际的,应予以避免。应该考虑在Spring bean 配置文件中定义电子邮件模板。
1.Spring的邮件发件人
Java类使用 Spring的MailSender接口发送电子邮件,并使用 String.Format 传递变量bean配置文件替换电子邮件中的 '%s'。

File : MailMail.java

package com.yiibai.common;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage; public class MailMail
{
private MailSender mailSender;
private SimpleMailMessage simpleMailMessage; public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
} public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
} public void sendMail(String dear, String content) { SimpleMailMessage message = new SimpleMailMessage(simpleMailMessage); message.setText(String.format(
simpleMailMessage.getText(), dear, content)); mailSender.send(message); }
}

2. Bean的配置文件

定义电子邮件模板“customeMailMessage' 和邮件发件人信息的bean配置文件。

File : Spring-Mail.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="username" />
<property name="password" value="password" /> <property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean> <bean id="mailMail" class="com.yiibai.common.MailMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean> <bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="from@no-spam.com" />
<property name="to" value="to@no-spam.com" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean> </beans>

4. 运行它

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml"); MailMail mm = (MailMail) context.getBean("mailMail");
mm.sendMail("Yiibai", "This is text content"); }
}

输出

Dear Yiibai,
Mail Content : This is text content
 

Spring在bean配置文件中定义电子邮件模板的更多相关文章

  1. 在Spring的Bean注入中,即使你私有化构造函数,默认他还是会去调用你的私有构造函数去实例化

    在Spring的Bean注入中,即使你私有化构造函数,默认他还是会去调用你的私有构造函数去实例化. 如果我们想保证实例的单一性,就要在定义<bean>时加上factory-method=” ...

  2. 在Spring框架中bean配置文件中constructor-arg标签中没有name元素?

    bean配置文件出现错误的依赖: <beans <beans xmlns="http://www.springframework.org/schema/beans"   ...

  3. 从基础知识到重写Spring的Bean工厂中学习java的工厂模式

    1.静态工厂模式其他对象不能直接通过new得到某个类,而是通过调用getInstance()方法得到该类的对象这样,就可以控制类的产生过程.顺带提一下单例模式和多例模式:  单例模式是指控制其他对象获 ...

  4. Spring的xml配置文件中约束的必要性 找不到元素 'beans' 的声明

    今天在复习Spring MVC框架的时候,只知道xml配置文件中的约束有规范书写格式的作用,所以在配置HandlerMapping对象信息的时候没有加入约束信息之后进行测试,没有遇到问题.后来在配置S ...

  5. spring boot: 从配置文件中读取数据的常用方法(spring boot 2.3.4)

    一,从配置文件中读取数据有哪些方法? 通常有3种用法: 1,直接使用value注解引用得到配置项的值 2,  封装到Component类中再调用 3,  用Environment类从代码中直接访问 生 ...

  6. 如何在WPF中定义窗体模板

    参考网址:https://www.cnblogs.com/chenxizhang/archive/2010/01/10/1643676.html可以在app.xaml中定义一个ControlTempl ...

  7. Mybatis在非spring环境下配置文件中使用外部数据源(druidDatasource)

    Spring环境下, MyBatis可以通过其本身的增强mybatis-spring提供的org.mybatis.spring.SqlSessionFactoryBean来注入第三方DataSourc ...

  8. 在controller中无法通过注解@Value获取到配置文件中定义的值

    1. 默认的我们通常只在dao层用到jdbc的配置,然后使用到@Value注解获取到值. 这时候会在spring-dao扫描中加入下面配置 <context:property-placehold ...

  9. 解决spring中不同配置文件中存在name或者id相同的bean可能引起的问题

    小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...

随机推荐

  1. Vim的分屏功能

    本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 f ...

  2. java并发容器

    同步容器将所有对容器状态的访问都串行化,以实现线程安全性.这种方式的缺点是严重降低并发性.Java 5.0提供了多种并发容器来改进同步容器的性能.如ConcurrentHashMap代替同步且基于散列 ...

  3. htaccess附录:正则表达式、重定向代码

    .htaccess正则表达式 # 位于行首时表示注释. [F] Forbidden(禁止): 命令服务器返回 403 Forbidden错误给用户浏览器 [L] Last rule(最后一条规则): ...

  4. csu 1803(余数分类)

    1803: 2016 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 565  Solved: 364[Submit][Status][Web Board ...

  5. 《HBase权威指南》学习笔记

    第一章  简介 背景: GFS:集群存储海量数据,数据在节点间冗余复制,即使一台存储服务器发生故障,也不会影响可用性. GFS的缺点:适合存储少许非常大的文件,而不适合存储大量小文件,因为文件的元数据 ...

  6. 找到最大或最小的N个元素---heapq模块

    堆排序heapq的用法 基本用法: 复杂数据结构: # coding=utf- # example.py # Example of using heapq to find the N smallest ...

  7. import xxx from 和 import {xxx} from的区别

    1.vue import FunName from ‘../xxx’ 1.js export defualt function FunName() { return fetch({ url: '/ar ...

  8. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E - Goods transportation 最大流转最小割转dp

    E - Goods transportation 思路:这个最大流-> 最小割->dp好巧妙哦. #include<bits/stdc++.h> #define LL long ...

  9. linux下php pcntl_fork模拟多线程

    开始用php写后台服务一段时间了.也是在这样的驱动下,不断的学习php语法,体验这一原来一直以为神秘且敬而远之的神奇语言的魅力.最初看php多线程的资料是为了提高程序的处理能力,充分发挥linux多任 ...

  10. 转:The Great DOM Fuzz-off of 2017

    The Great DOM Fuzz-off of 2017 Posted by Ivan Fratric, Project Zero https://googleprojectzero.blogsp ...