Spring在bean配置文件中定义电子邮件模板

简介:
在上一篇 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
 
代码下载 –   http://pan.baidu.com/s/1c0UPsFA
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/6367590.html,如需转载请自行联系原作者
相关文章
|
1天前
|
安全 Java Spring
Spring框架中的单例Bean是线程安全的吗?
Spring框架中的单例Bean是线程安全的吗?
10 1
|
1天前
|
XML JavaScript Java
【JavaEE】Spring Boot - 配置文件
【JavaEE】Spring Boot - 配置文件
7 0
|
1天前
|
XML 前端开发 Java
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
4 0
|
1天前
|
存储 缓存 Java
【JavaEE】Spring中注解的方式去获取Bean对象
【JavaEE】Spring中注解的方式去获取Bean对象
2 0
|
1天前
|
存储 Java 对象存储
【JavaEE】Spring中注解的方式去存储Bean对象
【JavaEE】Spring中注解的方式去存储Bean对象
6 0
|
1天前
|
存储 Java 对象存储
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
9 0
|
1天前
|
Java 开发者 Spring
Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
【5月更文挑战第1天】Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
25 5
|
1天前
|
XML Java 数据格式
如何在Spring AOP中定义和应用通知?
【4月更文挑战第30天】如何在Spring AOP中定义和应用通知?
17 0
|
1天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
21 1
|
1天前
|
XML Java 数据格式
Spring Bean
【4月更文挑战第30天】Spring Bean
17 0