In the previous spring email tutorial, hard-coded contents of all e-mail properties and the method body of the message, which is not practical, should be avoided. You should consider defining e-mail templates in the Spring bean configuration file. The 1.Spring message sender Java class uses the spring MailSender interface to send e-mail messages and replaces the '%s ' in the e-mail message with the String.Format pass variable bean configuration file.
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. The bean configuration file defines the e-mail template "Customemailmessage" and the bean profile for the message sender information.
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/beanshttp:// 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> </pr Ops></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=" [email Protected] "/><property name=" to "value=" [email protected] "/><property name=" subject "value=" Testing Subject "/><property name=" text "> <value><! [Cdata[dear%s,mail Content:%s]]> </value> </property></bean></beans>
4. Run it
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 the text content");} }
Output
Dear Yiibai, Mail content:this is text Content
Code Download –HTTP://PAN.BAIDU.COM/S/1C0UPSFA
Spring defines the e-mail template in the Bean configuration file