Spring defines the e-mail template in the Bean configuration file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.