Send a message using a mail template (Freemarker.jar)

Source: Internet
Author: User

1. Add the Freemarker.jar rack package to the project

2. Create the template file, named after *.FTL (the suffix is FTL), and the template shows for example

123<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >45<body>6<font color= "Blue" size= "4" face= "Arial" >7 Dear ${user}:8</font>9<br/><br/>Ten<font color= "Blue" size= "4" face= "Arial" > One&nbsp;&nbsp: Congratulations on your use of the system! Please keep your account number, if you forgot your password at login ACan be retrieved on the website login page. <br/> -<br/><br/>system Administrator -</font> the</body> -

3. Sending an email sample

1  Packagecn.bd.mail.template;2 3 Importjava.io.IOException;4 ImportJava.util.HashMap;5 ImportJava.util.Map;6 7 Importjavax.mail.MessagingException;8 ImportJavax.mail.internet.MimeMessage;9 Ten ImportOrg.springframework.mail.javamail.JavaMailSender; One ImportOrg.springframework.mail.javamail.MimeMessageHelper; A Importorg.springframework.ui.freemarker.FreeMarkerTemplateUtils; - Importfreemarker.template.Configuration; - Importfreemarker.template.Template; the Importfreemarker.template.TemplateException; -  -  Public classMailservice { -     PrivateJavamailsender MailSender; +     PrivateConfiguration freemarkerconfiguration; -      Public voidSetmailsender (Javamailsender mailsender) { +          This. MailSender =MailSender; A     } at      Public voidsetfreemarkerconfiguration (Configuration freemarkerconfiguration) { -          This. freemarkerconfiguration =freemarkerconfiguration; -     } -      -     PrivateString Getmailtext () { -String htmltext= ""; in         Try{ -             //Get template Instance toTemplate template=freemarkerconfiguration.gettemplate ("MAIL.FTL");//MAIL.FTL is the template file +             //delivering Dynamic Data via map -Map map=NewHashMap (); theMap.put ("User", "test"); *             //Parsing template Files $htmltext=freemarkertemplateutils.processtemplateintostring (template, map);Panax Notoginseng}Catch(IOException e) { - e.printstacktrace (); the}Catch(templateexception e) { + e.printstacktrace (); A}Catch(Exception e) { the e.printstacktrace (); +         } -         returnHTMLText; $     } $      -      Public voidSendMail ()throwsmessagingexception{ -MimeMessage mimemessage=mailsender.createmimemessage (); theMimemessagehelper helper=NewMimemessagehelper (MimeMessage,true, "UTF-8"); -Helper.setfrom ("[email protected]");//Sending PersonWuyiHelper.setto ("[email protected]");//Mail Recipient theHelper.setsubject ("title ....")); -Helper.settext (Getmailtext (),true);//set up HTML-formatted message content Wu mailsender.send (mimemessage); -     } About      $}

Configuration of the 4.Spring container

1<?xml version= "1.0" encoding= "UTF-8"?>2<Beans3Xmlns= "Http://www.springframework.org/schema/beans"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5xmlns:p= "http://www.springframework.org/schema/p"6Xsi:schemalocation= "Http://www.springframework.org/schema/beans7http//www.springframework.org/schema/beans/spring-beans-3.1.xsd ">8 9<bean id= "MailSender"class= "Org.springframework.mail.javamail.JavaMailSenderImpl" >Ten<property name= "host" value= "smtp.163.com" ></property> <!--server name-- One<property name= "Port" value= "></property> <!--port number-- A<property name= "username" value= "13599057417" ></property><!--user name-- -<property name= "Password" value= "Luofuxiang" ></property><!--password-- -<property name= "Protocol" value= "SMTP" ></property> <!--protocols-- the<property name= "defaultencoding" value= "Utf-8" ></property> -<property name= "Javamailproperties" > -<props> -<prop key= "Mail.smtp.auth" >true</prop> +</props> -</property> +</bean> A<bean id= "Freemarkerconfiguration"class= "Org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean" > at<property name= "Templateloaderpath" value= "/cn/bd/mail/template" ></property> <!--specify template (MAIL.FTL) path- - -<!--setting Freemarker environment Variables-- -<property name= "Freemarkersettings" > -<props> -<prop key= "Default_encoding" >UTF-8</prop> -</props> in</property> -</bean> to<!--configuration Bean:mailservice-- +<bean id= "Mailservice"class= "Cn.bd.mail.template.MailService" > -<property name= "MailSender" ref= "MailSender" ></property><!--inject values into a property-- the<property name= "freemarkerconfiguration" ref= "freemarkerconfiguration" ></property><!--injecting values into the attribute-- > *</bean> $</beans>

Send mail using message templates (Freemarker.jar)

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.